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.
244
u/zx48 15h ago •
In case the first answer isn't accepted.
99
u/Heavenfall 18h ago
Who said to output it to log?
20
u/OppositeFun2493 18h ago
r/technicallycorrect
37
u/Dense_Citron9715 17h ago
Ah, good old days in 2018 when we used to write pattern printing programs
23
u/da_Aresinger 15h ago
what an absolute bitch of a pattern.
This is so much more than just nested loops.
8
u/Baanloh 16h ago
25
u/Pure_Material_9235 18h 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.
3
u/sambeau 12h ago
I’m not sure this is correct. Wouldn’t this print the tabs/spaces on the start of each line?
2
u/leropo00 13h 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
2
u/BubblyAmbassador1039 10h ago
Not completely correct because the box is 10x9 in the exercise
2
u/Ulrar 7h ago
Criticical issue failing the gate: Cognitive complexity above 15
2
u/ZZerker 5h ago
Technically correct, the best kind of correct
1
u/Guerfel 3h ago
Not only correct, but also with a complexity of O(1), unbeatable
0
u/surykum36 8h ago
Task completed, learning objectives not found....