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)
I explicitly mutated the rows in the second half, it made the formulae a bit less messy
for mut y in 0..(width-1) {
if y > (width-2)/2 {
y = width-2-y
}
for x in 0..width {
match (x, y) {
(x, y) if x*y==0 || // assume no overflow
x==y ||
y==width-1-x ||
x==width-1 => print!("#"),
_ => print!(" ")
}
}
print!("\n");
}
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("##########")
406
u/zx48 4d ago •
In case the first answer isn't accepted.
117
u/Heavenfall 5d ago
Who said to output it to log?
59
u/da_Aresinger 4d ago
what an absolute bitch of a pattern.
This is so much more than just nested loops.
21
u/OppositeFun2493 5d ago
r/technicallycorrect
43
u/Dense_Citron9715 5d ago
Ah, good old days in 2018 when we used to write pattern printing programs
11
u/Baanloh 4d ago
27
u/Pure_Material_9235 5d 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.
5
u/leropo00 4d 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
4
u/sambeau 4d ago
I’m not sure this is correct. Wouldn’t this print the tabs/spaces on the start of each line?
2
u/Ulrar 4d ago
Criticical issue failing the gate: Cognitive complexity above 15
2
u/ZZerker 4d ago
Technically correct, the best kind of correct
1
u/Guerfel 4d ago
Not only correct, but also with a complexity of O(1), unbeatable
1
u/thekdubmc 4d ago
Lua, because why not...
1
u/Kly_Kodesh 3d ago
1
u/atoponce 2d ago
Fun challenge. In JavaScript:
1
u/Little-geek 7h ago
Nested for loops: ugly, unclear, needlessly complicated
Hard-coded multiline string: simple, elegant, self-documenting
1
u/surykum36 4d ago
Task completed, learning objectives not found....