Last active
August 29, 2015 14:05
-
-
Save boxmein/00fb280fcf3f56c73d3e to your computer and use it in GitHub Desktop.
An attempt to solve all Week 1 and 2 problems on a Python course with one-liners. (Task 1 was a graph!)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(lambda s, t, u: | |
print('Hello, ' + u + '-year-old ' + s + ' ' + t + '. Good luck on the course!'))( | |
input('First name? :: '), | |
input('Last name? :: '), | |
input('Age? :: ')) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Calculate the circumference of the Earth in lemonade bottles. | |
from math import pi | |
(lambda k, h, y: | |
print('You\'d need {:,.2f} bottles.'.format(y/k)) or | |
print('And these bottles could be reclaimed for {:,.2f} euros.'.format((y/k)*h)) | |
)(float(input('The length of a lemonade bottle? (cm) :: '))/100., | |
float(input('The reclaim price for a lemonade bottle? (eur) :: ')), 6378000*2*pi) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Read some ASCII art off the web and print it | |
from urllib.request import urlopen | |
(lambda f: | |
print(f.read().decode('UTF-8')))( | |
urlopen('https://dl.dropboxusercontent.com/u/22100200/txt/ascii-art.txt')) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Calculate time span until a specific time | |
from datetime import datetime, date, time | |
# datetime(days=,seconds=,microseconds=,milliseconds=,minutes=,hours=,weeks=) | |
(lambda n, t: | |
print(n + ', your show will start in ' + str(t.seconds//3600) + ' hours, '+ | |
str((t.seconds//60)%60) + ' minutes and ' + str(divmod(divmod(t.seconds, 3600)[1], 60)[1]) + ' seconds.') | |
)(input('What\'s your name? :: '), | |
datetime.combine(datetime.now().date(), | |
time(int(input('When will your show start? (hours) :: ')), | |
int(input('When will your show start? (minutes) :: ')))) - datetime.now()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Task 2: the only one I didn't manage to solve as a oneliner! | |
# It's a sort of branching story | |
print ('You wake up and see a wolf') | |
if input('Do you want to follow the wolf? :: (yes/no) ').lower() == 'yes': | |
print ('The wolf escapes, but you can track it down') | |
else: | |
print ('The wolf goes along') | |
print ('You\'re becoming hungry. How long have you been here...?') | |
if input('Track the wolf? :: (yes/no) ').lower() == 'no': | |
print ('The wolf meets a pack and they advance towards you. You have no option but to run away. After running for a while you drop down and the wolves reach you') | |
else: | |
print('You instead wander around the forest, and find a bush with lots of red berries on them') | |
if input('You pick some of the berries. Will you eat them? :: (yes/no) ').lower() == 'yes': | |
print ('The berries were poisonous! You feel drowsy and faint again.') | |
else: | |
print('You become more and more hungry. Soon you\'re way too tired to walk so you sit down and take a nice long nap..') | |
# ...jah, minestamine, nimetame seda selleks siis | |
if __name__=='__main__': | |
input() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Task 3: Make a password based lock | |
(lambda r: | |
r(r,'?drowssaP',1) | |
)(lambda r,n,c: | |
input('Password? :: ' if c==1 else 'Wrong password! Read the source code! \r\nPassword? :: ') == n and | |
(lambda: print('yay') or True)() or | |
(c == 3 and | |
(lambda: print('Out of tries - you didn\'t make it this time!') or True)() | |
or r(r,n,c+1))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from time import sleep | |
# Task 4: A timer with two inputs | |
# Count down from B to A in time and say "Ran out of time!" when done | |
(lambda a, b: [sleep(1) or print(i) for i in range(a, b-1, -1)] and print('Ran out of time!'))( | |
int(input('Starting from the number? :: ')), int(input('Ends with the number? :: '))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Task 5: Find the sum of all numbers from 100 to 2000, that can be divided by 3. | |
print(sum(range(102, 2000, 3))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment