Skip to content

Commit

Permalink
Refactor source code
Browse files Browse the repository at this point in the history
Fixes kyclark#1
Use map function when assigning low, high in read_csv to split the string at the '-' character, and convert the strings to integers using int() function, the first map argument.

Fixes kyclark#2
Initiate empty list as wod. Use a foor loop of random.sample(exercises, ...)  which creates a list of tuples. Assign 3 variables for each iteration of the loop: exercise, low, and high.

Pick a random number between low and high, and append the tuple to wod. If the easy flag is given as argument, divide reps by two. Use the int() function to convert the result to an integer.
  • Loading branch information
Yarpirates20 committed Dec 23, 2022
1 parent f604bf2 commit 2e502ef
Showing 1 changed file with 23 additions and 11 deletions.
34 changes: 23 additions & 11 deletions 19_wod/wod.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,28 @@ def main():
args = get_args()
random.seed(args.seed)
exercises = read_csv(args.file)
wod = random.sample(exercises, k=args.num)
# print(wod) #debug 1
new_wod = []
for exercise in wod:
name = exercise[0]
chosen_reps = random.randint(exercise[1], exercise[2])

wod = []
for exercise, low, high in random.sample(exercises, k=args.num):
reps = random.randint(low, high)
if args.easy:
chosen_reps = int(chosen_reps / 2)
reps = int(reps / 2)
wod.append((exercise, reps))

print(tabulate(wod, headers=('Exercise', 'Reps')))

##### MY ORIGINAL SOLUTION ######
# wod = random.sample(exercises, k=args.num)
# new_wod = []
# for exercise in wod:
# name = exercise[0]
# chosen_reps = random.randint(exercise[1], exercise[2])
# if args.easy:
# chosen_reps = int(chosen_reps / 2)

new_wod.append((name, chosen_reps))
# new_wod.append((name, chosen_reps))

print(tabulate(new_wod, headers=('Exercise', 'Reps'))) # debug 2
# print(tabulate(new_wod, headers=('Exercise', 'Reps')))

# random.sample(exercises, k=args.num)

Expand All @@ -86,8 +96,10 @@ def read_csv(fh):
exercises = []
for rec in reader:
name, reps = rec['exercise'], rec['reps']
match = re.match(r'(\d+)-(\d+)', reps)
low, high = int(match.group(1)), int(match.group(2))
### MY ORIGINAL SOLUTION ####
# match = re.match(r'(\d+)-(\d+)', reps)
# low, high = int(match.group(1)), int(match.group(2))
low, high = map(int, reps.split('-'))
exercises.append((name, low, high))

return exercises
Expand Down

0 comments on commit 2e502ef

Please sign in to comment.