If an interviewee gave me this answer I would seriously lose my train of thought. Obviously not what was intended but it conveys your understanding well AND it seems like a more technically correct solution from a requirements perspective. amazing answer lol
I actually used to kick off phone screens with asking how they would print out a multiplication table. A nested for loop was the correct answer, but suggesting hard-coding the tables (and better yet, explaining when that would be appropriate and when it wouldn’t) was some bonus points.
Yeah 100%, outside the box A+ answer, if someone gave this to me in an interview I'd be very impressed. Its the work of an idiot or a genius and that's worth pursuing.
It kind of bothers me how the variable result is redundant. You could have just printed each character directly instead of accumulating them in a string.
Ok this actually was a bit harder than I imagined when I realized that it was not a square. Truly weird.
def inline(text):
print(text, end='')
def pattern(width):
for x in range(width - 1):
for y in range(width):
if x == 0 or x == width - 2 or y == 0 or y == width - 1:
inline("#")
elif x < width // 2 and (x == y or x == width - y - 1):
inline("#")
elif x >= width // 2 and (y == x + 1 or y == width - x - 2):
inline("#")
else:
inline(" ")
print()
pattern(10)
Who would have thought the meme is poorly thought out. Regardless, it doesn't add all of the complexity you implied. The fix is one more Boolean and updating the diagonal check
let isLowerHalf = y > maxY / 2;
let isDiagonal = x == y + isLowerHalf || x == maxX - y - isLowerHalf;
I typed it in 20 seconds on my phone on JS fiddle then had to retype it because the copy+paste doesn't work on jsfiddled mobile site and OCR are ligatures.
Chained conditionals CAN be a code smell, but when used sparingly they can help reduce cognitive complexity.
The version a beginner would make is a version with 7 or 8 separate booleans and multiple print statements which is besides the point. Logically it is the same.
Ignoring that, the person I was replying to didn't know how to do it and explicitly said it needed a bunch of extra nonsense that it did not need. They were trying to be clever and failed.
The @ symbol in C# means that you don't have to escape special characters inside, including new lines. This is why you can have a string spanning a few lines without syntax error.
print("##########")
for a = 0, 6, 1 do
local o = 3-math.abs(a-3)
local i = (3-o)*2
io.write("#")
for b = 0, o-1, 1 do io.write(" ") end
io.write("#")
for c = 0, i-1, 1 do io.write(" ") end
io.write("#")
for d = 0, o-1, 1 do io.write(" ") end
print("#")
end
print("##########")
329
u/zx48 1d ago •
In case the first answer isn't accepted.
106
u/Heavenfall 1d ago
Who said to output it to log?
20
u/OppositeFun2493 1d ago
r/technicallycorrect
33
u/da_Aresinger 1d ago
what an absolute bitch of a pattern.
This is so much more than just nested loops.
38
u/Dense_Citron9715 1d ago
Ah, good old days in 2018 when we used to write pattern printing programs
9
u/Baanloh 1d ago
26
u/Pure_Material_9235 1d ago
Does this ensure that it only prints it one time? It seems like this would print it many times. Does that @ symbol make it only print once?
Oh, nevermind, it's j = 0, i = 0, and both less than 1.
4
u/sambeau 1d ago
I’m not sure this is correct. Wouldn’t this print the tabs/spaces on the start of each line?
3
u/leropo00 1d ago
TIL, i learned there is no pattern that For loop can't create in a single iteration. Sounds super fancy, but seems obvious when looking back :D
3
u/BubblyAmbassador1039 1d ago
Not completely correct because the box is 10x9 in the exercise
2
u/Ulrar 1d ago
Criticical issue failing the gate: Cognitive complexity above 15
2
u/ZZerker 1d ago
Technically correct, the best kind of correct
1
u/Guerfel 1d ago
Not only correct, but also with a complexity of O(1), unbeatable
1
u/thekdubmc 1d ago
Lua, because why not...
1
u/Kly_Kodesh 17h ago
1
u/surykum36 1d ago
Task completed, learning objectives not found....