Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Python 

[py challenge] challenge #2

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
(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.
Spoiler (Click to View)
(Dec 23, 2013 02:29 PM)glob Wrote:  Spoiler (Click to View)

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
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.
Spoiler (Click to View)
(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.
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 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.
Spoiler (Click to View)

Damn. Today I learned.
Also, behold my generic solutions :

Spoiler (Click to View)
(Dec 23, 2013 04:11 PM)glob Wrote:  A lambda is an anonymous function. Call it the same way lol
Spoiler (Click to View)

But nevermind, just peanuts 😁


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Python] by -MaxFeb 11, 2013 01:17 PM 13 664 Feb 11, 2013 01:17 PM
Last Post: frenchtoast