Hack Forums
[Python] [py challenge] challenge #2 - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Python] [py challenge] challenge #2 (/showthread.php?tid=3947066)



[py challenge] challenge #2 - SupreemJuggalo - Dec 23, 2013

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


* - 1nf4m0us - Dec 23, 2013

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)


* - SupreemJuggalo - Dec 23, 2013

(Dec 23, 2013 01:55 PM)tokivena 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


* - ImportUsername - Dec 23, 2013

(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


* - SupreemJuggalo - Dec 23, 2013

sorry, i did not make it very clear. you still do need to print the answer. but it can not happen within the function.


* - glob - Dec 23, 2013

Spoiler (Click to View)


* - Skyla Blue - Dec 23, 2013

(Dec 23, 2013 02:29 PM)glob Wrote:  Spoiler (Click to View)

Damnit glob, you and your obsession with lambadas ._.


* - Melkor333 - Dec 23, 2013

glob that must be in a function.. =P


* - Skyla Blue - Dec 23, 2013

(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


* - PigBacon - Dec 23, 2013

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)


* - glob - Dec 23, 2013

(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


* - SupreemJuggalo - Dec 24, 2013

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.


* - DpzFAsq4UEK8 - Dec 24, 2013

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) 



* - Eight - Dec 24, 2013

(Dec 23, 2013 03:16 PM)plausibility 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)


* - Melkor333 - Dec 24, 2013

(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 😁