Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions codingbat_exercise_scraper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
"""
This script gets all the exercises in codingbat and creates a base file in the appropriate folder to edit.

You need to install requests and bs4.

You can do the following to install:
pip install bs4
pip install requests

Author: Ha Min Ko
"""

import requests, bs4
import os
from os.path import exists

urlbase = 'https://codingbat.com'
url = urlbase + '/python'

cwd = os.getcwd()
res = requests.get(url)
res.raise_for_status()

html = bs4.BeautifulSoup(res.text, features='html.parser')

# We get the link for each main page.
for link in html.select('div[class=summ] > a'):
name = link.text.lower()
folder = cwd + '\\' + name
os.chdir(folder)

url1 = urlbase + link['href']

res1 = requests.get(url1)
html1 = bs4.BeautifulSoup(res1.text, features='html.parser')

# We get the link for each exercise.
for link1 in html1.select('td[width="200"] > a'):
# print(link1.text)
name1 = link1.text

url2 = urlbase + link1['href']
res2 = requests.get(url2)

html2 = bs4.BeautifulSoup(res2.text, features='html.parser')
# print(html2)

title = name1 + '.py'

# Check if file already exists.
if (exists(title)):
print(title, 'exists already.')
else:
function_definer = html2.select('div[id=ace_div]')[0].text.strip()
function_description = html2.select('p[class=max2]')[0].text.strip()
function_test = []
for br in html2.find_all('br'):
following = br.nextSibling
if name1 in following:
function_test.append(following)

# Create a file that can be written to.
print('Creating file', title, 'in folder', name )
outputfile = open(title, 'w')

# Write data to the file:
outputfile.write(function_definer)
outputfile.write('\n\t"""')
outputfile.write('\n\t' + function_description)
outputfile.write('\n\t"""')
outputfile.write('\n\tpass')
outputfile.write('\n')
for test in function_test:
test = test.split('→')[0].strip()
outputfile.write('\nprint(' + test + ')')
outputfile.write('\n')

# Close the file after writing to it
outputfile.close()
9 changes: 9 additions & 0 deletions list-1/common_end.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
def common_end(a, b):
"""
Given 2 arrays of ints, a and b, return True if they have the same first element or they have the same last element. Both arrays will be length 1 or more.
"""
return a[-1] == b[-1] or a[0] == b[0]

print(common_end([1, 2, 3], [7, 3]))
print(common_end([1, 2, 3], [7, 3, 2]))
print(common_end([1, 2, 3], [1, 3]))
9 changes: 9 additions & 0 deletions list-1/has23.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
def has23(nums):
"""
Given an int array length 2, return True if it contains a 2 or a 3.
"""
return 2 in nums or 3 in nums

print(has23([2, 5]))
print(has23([4, 3]))
print(has23([4, 5]))
9 changes: 9 additions & 0 deletions list-1/make_ends.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
def make_ends(nums):
"""
Given an array of ints, return a new array length 2 containing the first and last elements from the original array. The original array will be length 1 or more.
"""
return [nums[0], nums[-1]]

print(make_ends([1, 2, 3]))
print(make_ends([1, 2, 3, 4]))
print(make_ends([7, 4, 6, 2]))
7 changes: 7 additions & 0 deletions list-1/make_pi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
def make_pi():
"""
Return an int array length 3 containing the first 3 digits of pi, {3, 1, 4}.
"""
return [3, 1, 4]

print(make_pi())
9 changes: 9 additions & 0 deletions list-1/max_end3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
def max_end3(nums):
"""
Given an array of ints length 3, figure out which is larger, the first or last element in the array, and set all the other elements to be that value. Return the changed array.
"""
return [max(nums[0], nums[-1]) for x in nums]

print(max_end3([1, 2, 3]))
print(max_end3([11, 5, 9]))
print(max_end3([2, 11, 3]))
9 changes: 9 additions & 0 deletions list-1/middle_way.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
def middle_way(a, b):
"""
Given 2 int arrays, a and b, each length 3, return a new array length 2 containing their middle elements.
"""
return [a[1], b[1]]

print(middle_way([1, 2, 3], [4, 5, 6]))
print(middle_way([7, 7, 7], [3, 8, 0]))
print(middle_way([5, 2, 9], [1, 4, 5]))
9 changes: 9 additions & 0 deletions list-1/reverse3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
def reverse3(nums):
"""
Given an array of ints length 3, return a new array with the elements in reverse order, so {1, 2, 3} becomes {3, 2, 1}.
"""
return nums[::-1]

print(reverse3([1, 2, 3]))
print(reverse3([5, 11, 9]))
print(reverse3([7, 0, 0]))
9 changes: 9 additions & 0 deletions list-1/rotate_left3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
def rotate_left3(nums):
"""
Given an array of ints length 3, return an array with the elements "rotated left" so {1, 2, 3} yields {2, 3, 1}.
"""
return nums[1:] + nums[:1]

print(rotate_left3([1, 2, 3]))
print(rotate_left3([5, 11, 9]))
print(rotate_left3([7, 0, 0]))
9 changes: 9 additions & 0 deletions list-1/same_first_last.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
def same_first_last(nums):
"""
Given an array of ints, return True if the array is length 1 or more, and the first element and the last element are equal.
"""
return len(nums) >= 1 and nums[0] == nums[-1]

print(same_first_last([1, 2, 3]))
print(same_first_last([1, 2, 3, 1]))
print(same_first_last([1, 2, 1]))
9 changes: 9 additions & 0 deletions list-1/sum2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
def sum2(nums):
"""
Given an array of ints, return the sum of the first 2 elements in the array. If the array length is less than 2, just sum up the elements that exist, returning 0 if the array is length 0.
"""
return sum(nums[0:2])

print(sum2([1, 2, 3]))
print(sum2([1, 1]))
print(sum2([1, 1, 1, 1]))
9 changes: 9 additions & 0 deletions list-1/sum3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
def sum3(nums):
"""
Given an array of ints length 3, return the sum of all the elements.
"""
return sum(nums)

print(sum3([1, 2, 3]))
print(sum3([5, 11, 2]))
print(sum3([7, 0, 0]))
9 changes: 9 additions & 0 deletions list-2/big_diff.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
def big_diff(nums):
"""
Given an array length 1 or more of ints, return the difference between the largest and smallest values in the array. Note: the built-in min(v1, v2) and max(v1, v2) functions return the smaller or larger of two values.
"""
return max(nums) - min(nums)

print(big_diff([10, 3, 5, 6]))
print(big_diff([7, 2, 10, 9]))
print(big_diff([2, 10, 7, 2]))
10 changes: 10 additions & 0 deletions list-2/centered_average.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
def centered_average(nums):
"""
Return the "centered" average of an array of ints, which we'll say is the mean average of the values, except ignoring the largest and smallest values in the array. If there are multiple copies of the smallest value, ignore just one copy, and likewise for the largest value. Use int division to produce the final average. You may assume that the array is length 3 or more.
"""
sort_nums = sorted(nums)
return sum(sort_nums[1:-1])/len(sort_nums[1:-1])

print(centered_average([1, 2, 3, 4, 100]))
print(centered_average([1, 1, 5, 5, 10, 8, 7]))
print(centered_average([-10, -4, -2, -4, -2, 0]))
12 changes: 12 additions & 0 deletions list-2/has22.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
def has22(nums):
"""
Given an array of ints, return True if the array contains a 2 next to a 2 somewhere.
"""
for i in range(len(nums) - 1):
if nums[i] == 2 and nums[i+1] == 2:
return True
return False

print(has22([1, 2, 2]))
print(has22([1, 2, 1, 2]))
print(has22([2, 1, 2]))
19 changes: 19 additions & 0 deletions list-2/sum13.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
def sum13(nums):
"""
Return the sum of the numbers in the array, returning 0 for an empty array. Except the number 13 is very unlucky, so it does not count and numbers that come immediately after a 13 also do not count.
"""
total = 0
is_after_13 = 0
for i in range(len(nums)):
if is_after_13 == 0 and nums[i] != 13:
total += nums[i]
elif nums[i] == 13:
is_after_13 = 1
else:
is_after_13 = 0
return total


print(sum13([1, 2, 2, 1]))
print(sum13([1, 1]))
print(sum13([1, 2, 2, 1, 13]))
18 changes: 18 additions & 0 deletions list-2/sum67.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
def sum67(nums):
"""
Return the sum of the numbers in the array, except ignore sections of numbers starting with a 6 and extending to the next 7 (every 6 will be followed by at least one 7). Return 0 for no numbers.
"""
total = 0
is_after_6 = 0
for i in range(len(nums)):
if nums[i] == 6:
is_after_6 = 1
elif nums[i] == 7 and is_after_6 == 1:
is_after_6 = 0
elif is_after_6 != 1:
total += nums[i]
return total

print(sum67([1, 2, 2]))
print(sum67([1, 2, 2, 6, 99, 99, 7]))
print(sum67([1, 1, 6, 7, 2]))
16 changes: 16 additions & 0 deletions logic-1/alarm_clock.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
def alarm_clock(day, vacation):
"""
Given a day of the week encoded as 0=Sun, 1=Mon, 2=Tue, ...6=Sat, and a boolean indicating if we are on vacation, return a string of the form "7:00" indicating when the alarm clock should ring. Weekdays, the alarm should be "7:00" and on the weekend it should be "10:00". Unless we are on vacation -- then on weekdays it should be "10:00" and weekends it should be "off".
"""
if day in [1,2,3,4,5]:
if vacation:
return '10:00'
return '7:00'
else:
if vacation:
return 'off'
return '10:00'

print(alarm_clock(1, False))
print(alarm_clock(5, False))
print(alarm_clock(0, False))
22 changes: 22 additions & 0 deletions logic-1/caught_speeding.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
def caught_speeding(speed, is_birthday):
"""
You are driving a little too fast, and a police officer stops you. Write code to compute the result, encoded as an int value: 0=no ticket, 1=small ticket, 2=big ticket. If speed is 60 or less, the result is 0. If speed is between 61 and 80 inclusive, the result is 1. If speed is 81 or more, the result is 2. Unless it is your birthday -- on that day, your speed can be 5 higher in all cases.
"""
if not is_birthday:
if speed <= 60:
return 0
elif speed <= 80:
return 1
else:
return 2
else:
if speed <= 65:
return 0
elif speed <= 85:
return 1
else:
return 2

print(caught_speeding(60, False))
print(caught_speeding(65, False))
print(caught_speeding(65, True))
14 changes: 14 additions & 0 deletions logic-1/date_fashion.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
def date_fashion(you, date):
"""
You and your date are trying to get a table at a restaurant. The parameter "you" is the stylishness of your clothes, in the range 0..10, and "date" is the stylishness of your date's clothes. The result getting the table is encoded as an int value with 0=no, 1=maybe, 2=yes. If either of you is very stylish, 8 or more, then the result is 2 (yes). With the exception that if either of you has style of 2 or less, then the result is 0 (no). Otherwise the result is 1 (maybe).
"""
if you <=2 or date <= 2:
return 0
elif you >= 8 or date >= 8:
return 2
else:
return 1

print(date_fashion(5, 10))
print(date_fashion(5, 2))
print(date_fashion(5, 5))
9 changes: 9 additions & 0 deletions logic-1/in1to10.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
def in1to10(n, outside_mode):
"""
Given a number n, return True if n is in the range 1..10, inclusive. Unless outside_mode is True, in which case return True if the number is less or equal to 1, or greater or equal to 10.
"""
return (n in range(1, 11)) is not outside_mode or (n not in range(2, 10)) is outside_mode

print(in1to10(5, False))
print(in1to10(11, False))
print(in1to10(11, True))
9 changes: 9 additions & 0 deletions logic-1/love6.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
def love6(a, b):
"""
The number 6 is a truly great number. Given two int values, a and b, return True if either one is 6. Or if their sum or difference is 6. Note: the function abs(num) computes the absolute value of a number.
"""
return a == 6 or b == 6 or a+b == 6 or abs(a-b) == 6

print(love6(6, 4))
print(love6(4, 5))
print(love6(1, 5))
9 changes: 9 additions & 0 deletions logic-1/near_ten.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
def near_ten(num):
"""
Given a non-negative number "num", return True if num is within 2 of a multiple of 10. Note: (a % b) is the remainder of dividing a by b, so (7 % 5) is 2. See also: Introduction to Mod
"""
return num%10 <= 2 or num%10 >= 8

print(near_ten(12))
print(near_ten(17))
print(near_ten(19))
12 changes: 12 additions & 0 deletions logic-1/sorta_sum.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
def sorta_sum(a, b):
"""
Given 2 ints, a and b, return their sum. However, sums in the range 10..19 inclusive, are forbidden, so in that case just return 20.
"""
if a + b in range(10, 20):
return 20
else:
return a + b

print(sorta_sum(3, 4))
print(sorta_sum(9, 4))
print(sorta_sum(10, 11))
9 changes: 9 additions & 0 deletions logic-1/squirrel_play.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
def squirrel_play(temp, is_summer):
"""
The squirrels in Palo Alto spend most of the day playing. In particular, they play if the temperature is between 60 and 90 (inclusive). Unless it is summer, then the upper limit is 100 instead of 90. Given an int temperature and a boolean is_summer, return True if the squirrels play and False otherwise.
"""
return temp in range (60, 91) or ((temp in range(60, 101)) and is_summer)

print(squirrel_play(70, False))
print(squirrel_play(95, False))
print(squirrel_play(95, True))
9 changes: 9 additions & 0 deletions logic-2/close_far.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
def close_far(a, b, c):
"""
Given three ints, a b c, return True if one of b or c is "close" (differing from a by at most 1), while the other is "far", differing from both other values by 2 or more. Note: abs(num) computes the absolute value of a number.
"""
return (abs(a - b) <= 1 and abs(a - c) >= 2 or abs(a - c) <= 1 and abs(a - b) >= 2) and abs(b - c) >= 2

print(close_far(1, 2, 10))
print(close_far(1, 2, 3))
print(close_far(4, 1, 3))
14 changes: 14 additions & 0 deletions logic-2/lucky_sum.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
def lucky_sum(a, b, c):
"""
Given 3 int values, a b c, return their sum. However, if one of the values is 13 then it does not count towards the sum and values to its right do not count. So for example, if b is 13, then both b and c do not count.
"""
val = 0
for num in [a, b, c]:
if num == 13:
break
val += num
return val

print(lucky_sum(1, 2, 3))
print(lucky_sum(1, 2, 13))
print(lucky_sum(1, 13, 3))
Loading