hey all, this is to test your python skills (if i get good feedback from this i will post more)
1. Write a function is_between(x, y, z) that returns True if x ≤ y ≤ z or False otherwise.
2. Write a function factorial that takes in a number, calculates it’s factorial and returns the final value to the program to print. The print should not happen within the function. Your function should use recursion, not extensive calculations.
please post the code in pastebin or if you must you can use the "code" tags
1) def is_between(x, y, z): return y >= x and y <= z
2) def factorial(x): return 1 if x==1 else x * factorial(x-1)
print factorial(4)
(Dec 23, 2013 01:55 PM)1nf4m0us Wrote: 1) def is_between(x, y, z): return y >= x and y <= z
2) def factorial(x, total): return total if x==1 else factorial(x-1, total*x)
print factorial(4, 1)
you are correct but it would have been easier to read if you put it in code tags or upload it to paste bin. :p
also you did not print weather its true or false for question 1
DO NOT DEAL WITH THIS MEMBER
(Dec 23, 2013 02:01 PM)SupreemJuggalo Wrote: also you did not print weather its true or false for question 1
You just says to return the answer, not print it
sorry, i did not make it very clear. you still do need to print the answer. but it can not happen within the function.
PHP Code
<?php is_between = lambda x,y,z: x<=y<=z fact = lambda x: 1 if not x else x * fact(x-1) print is_between(1,2,3)
(Dec 23, 2013 02:29 PM)glob Wrote:
PHP Code
<?php is_between = lambda x,y,z: all([x<=y,y<=z]) fact = lambda x: 1 if not x else x * fact(x-1) print is_between(1,2,3)
Damnit glob, you and your obsession with lambadas ._.
glob that must be in a function.. =P
(Dec 23, 2013 03:00 PM)Melkor333 Wrote: glob that must be in a function.. =P
Do you not know what a lambada is? Lol
DO NOT DEAL WITH THIS MEMBER
You guys are all making this way too complex. You can literally chain the comparison operands in Python. He gave you what you needed to do in the challenge.
>>> x, y, z = 1, 2, 3
>>> x <= y <= z
True
(Dec 23, 2013 03:00 PM)Melkor333 Wrote: glob that must be in a function.. =P
A lambda is an anonymous function. Call it the same way lol
i can see you all are having a little fun with this. lol thats good to see, i will post another one tomorrow. look for it if you like.
Dec 24, 2013 06:15 AM
(This post was last modified: Dec 24, 2013 08:26 AM by DpzFAsq4UEK8.)
1. def is_between(x,y,z): return x <= y <= z
2.
PHP Code
<?php def fa(num): if num == 0: return 1 elif num < 0: raise Exception("negative number!") else: return num * fa(num-1)
Dec 24, 2013 02:45 PM
(This post was last modified: Dec 24, 2013 02:46 PM by Eight.)
(Dec 23, 2013 03:16 PM)PigBacon Wrote: You guys are all making this way too complex. You can literally chain the comparison operands in Python. He gave you what you needed to do in the challenge.
>>> x, y, z = 1, 2, 3
>>> x <= y <= z
True
Damn. Today I learned.
Also, behold my generic solutions :
PHP Code
<?php def is_between(x, y, z): return x <= y <= z
def fact(n): if n == 1: return 1 return n * fact(n - 1)
print is_between(1, 2, 3) print fact(5)
(Dec 23, 2013 04:11 PM)glob Wrote: A lambda is an anonymous function. Call it the same way lol
okay, i wrote it wrong.. i meant that should all be in ONE function:
Code def is_between(x, y, z):
your
lambdas
here
is_between(1, 2, 3)
But nevermind, just peanuts 😁
|