reordered files to support more languagues than just rust, also added a

few python challenges
This commit is contained in:
nkoch001
2018-10-26 18:05:57 +02:00
parent fba80a7fc6
commit db882dd5dc
12 changed files with 547 additions and 0 deletions
+7
View File
@@ -0,0 +1,7 @@
5d12
6d4
1d2
1d8
3d6
4d20
100d100
+51
View File
@@ -0,0 +1,51 @@
from random import randint
def roll(number, sides):
"""
roll a number of virtual dice with a given number of sides
"""
res = 0
throws = []
for die in range(number):
throws.append(randint(1, sides))
res += throws[-1]
print(res, ":", *throws)
return res
def parselist(path):
"""
parse a list containing dice rolls (one per line) a la
5d12
6d4
1d2
1d8
3d6
4d20
100d100
"""
roll_call = []
with open(path, 'r') as rolls:
for line in rolls:
line = line.strip()
roll_call.append(line.split('d'))
return roll_call
if __name__ == '__main__':
pali = parselist('dice.rolls')
print(pali)
for to_roll in pali:
n, s = to_roll
print("Rolling", n + "d" + s)
print("Result:", roll(int(n), int(s)), '\n')
# interactive mode
x = 0
while x != 'x':
x = input("please input what you want to throw as 'NdM', or 'x' to quit\n")
if len(x.split('d')) == 2:
n, m = x.split('d')
roll(int(n), int(m))
+31
View File
@@ -0,0 +1,31 @@
def funnel_naive(string1, string2):
"""
detect if the second string can be made from the first by removing one letter
"""
if len(string1) - len(string2) != 1:
return False
if string2 in string1:
return True
miss = 0
for i, letter2 in enumerate(string2):
if letter2 != string1[i]:
if miss > 1:
return False
miss += 1
if letter2 == string1[i + 1]:
i += 1
else:
return False
return True
funnel = lambda word, other: other in [w[:i] + w[i+1:] for i, w in enumerate([word] * len(word))]
print(funnel_naive("leave", "leav"))
print(funnel("leave", "eave"))
print(funnel("reset", "rest"))
print(funnel("dragoon", "dragon"))
print(funnel("eave", "leave"))
print(funnel("sleet", "lets"))
print(funnel("skiff", "ski"))