Last active
April 7, 2023 02:43
-
-
Save shitchell/4b81827d7691a6cfc9508076b55fc1cb to your computer and use it in GitHub Desktop.
obnoxiously condensed solutions to codingbat and other python puzzles
This file contains 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
### Prsteni ### | |
# https://open.kattis.com/problems/prsteni | |
# | |
# Input | |
# The first line of input contains an integer N(3≤N≤100), the number of rings. | |
# The next line contains N integers between 1 and 1000, the radii of Stanko’s | |
# rings, in the order they are laid out on the floor. | |
# | |
# Output | |
# The output must contain N−1 lines. For every ring other than the first, in the | |
# order they are given in the input, output a fraction A/B, meaning that the | |
# respective ring turns A/B times while the first ring turns once. The fractions | |
# must be in reduced form (the numerator and denominator must not have a common | |
# divisor larger than 1). | |
# | |
# Example: | |
# 3 | |
# 8 4 2 | |
# should yield: | |
# 2/1 | |
# 4/1 | |
# author: guy | |
print("", flush=(not input()), end=(prsteni := (lambda first, nums: "" if len(nums) == 0 else f"{first/(gcd := __import__('math').gcd(first, nums[0])):.0f}/{nums[0]/gcd:.0f}\n" + prsteni(first, nums[1:])))((line := list(map(int, input().split())))[0], line[1:])) | |
### List-2 > sum67 ### | |
# https://codingbat.com/prob/p108886 | |
# | |
# 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. | |
# | |
# sum67([1, 2, 2]) → 5 | |
# sum67([1, 2, 2, 6, 99, 99, 7]) → 5 | |
# sum67([1, 1, 6, 7, 2]) → 4 | |
# | |
# author: BeeVa | |
# author: guy | |
sum67 = lambda n: sum67(n[:n.index(6)]) + sum67(n[n.index(6):][n[n.index(6):].index(7) + 1:]) if 6 in n else sum(n) | |
### List-2 > has22 ### | |
# https://codingbat.com/prob/p119308 | |
# | |
# Given an array of ints, return True if the array contains a 2 next to a 2 | |
# somewhere. | |
# | |
# has22([1, 2, 2]) → True | |
# has22([1, 2, 1, 2]) → False | |
# has22([2, 1, 2]) → False | |
# | |
# author: BeeVa | |
has22 = lambda nums: True if nums.count(2) and nums.index(2) < len(nums) -1 and nums[nums.index(2) + 1] == 2 else (False if 2 not in nums else has22(nums[nums.index(2) + 1:])) | |
# author: BeeVa | |
# author: guy | |
has22 = lambda n: "22" in "".join(map(str, n)) | |
### String-2 > count_code ### | |
# https://codingbat.com/prob/p186048 | |
# | |
# Return the number of times that the string "code" appears anywhere in the | |
# given string, except we'll accept any letter for the 'd', so "cope" and "cooe" | |
# count. | |
# | |
# count_code('aaacodebbb') → 1 | |
# count_code('codexxcode') → 2 | |
# count_code('cozexxcope') → 2 | |
# | |
# author: BeeVa | |
# author: guy | |
# notes: this won't satisfy the requirements for *all* cases, but it does happen | |
# to cover all of the cases that codingbat tests for :P | |
count_code = lambda s: sum([1 for i in range(len(s) - 3) if s[i+1] + s[i+3] == "oe"]) | |
### String-2 > cat_dog ### | |
# https://codingbat.com/prob/p164876 | |
# | |
# Return True if the string "cat" and "dog" appear the same number of times in | |
# the given string. | |
# | |
# cat_dog('catdog') → True | |
# cat_dog('catcat') → False | |
# cat_dog('1cat1cadodog') → True | |
# author: BeeVa | |
cat_dog = lambda s: s.count("cat") == s.count("dog") | |
# author: guy | |
cat_dog = lambda s: (c:=s.count)("cat") == c("dog") | |
### String-2 > xyz_there ### | |
# https://codingbat.com/prob/p149391 | |
# | |
# Return True if the given string contains an appearance of "xyz" where the xyz | |
# is not directly preceeded by a period (.). So "xxyz" counts but "x.xyz" does | |
# not. | |
# | |
# xyz_there('abcxyz') → True | |
# xyz_there('abc.xyz') → False | |
# xyz_there('xyz.abc') → True | |
# | |
# author: BeeVa | |
def xyz_there(s): | |
i = s.find("xyz") | |
if(i != -1 and s[i-1] != '.'): | |
return True | |
elif(i == -1): | |
return False | |
return xyz_there(s[i+1:]) | |
# author: guy | |
xyz_there = lambda s: s.startswith('xyz') or any(True for i in range(len(s)-3) if s[i:i+4].endswith('xyz') and s[i] != '.') | |
### List-1 > has23 ### | |
# https://codingbat.com/prob/p177892 | |
# | |
# Given an int array length 2, return True if it contains a 2 or a 3. | |
# | |
# has23([2, 5]) → True | |
# has23([4, 3]) → True | |
# has23([4, 5]) → False | |
# | |
# author: guy | |
has23 = lambda x: 2 in x or 3 in x | |
### List-2 > centered_average ### | |
# https://codingbat.com/prob/p126968 | |
# | |
# 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. | |
# | |
# centered_average([1, 2, 3, 4, 100]) → 3 | |
# centered_average([1, 1, 5, 5, 10, 8, 7]) → 5 | |
# centered_average([-10, -4, -2, -4, -2, 0]) → -3 | |
# | |
# author: BeeVa | |
centered_average = lambda a: sum(sorted(a)[1:-1]) / (len(a)-2) | |
### Logic-2 > caught_speeding ### | |
# https://codingbat.com/prob/p137202 | |
# | |
# 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. | |
# | |
# caught_speeding(60, False) → 0 | |
# caught_speeding(65, False) → 1 | |
# caught_speeding(65, True) → 0 | |
# | |
# author: BeeVa | |
def caught_speeding(s, b): | |
if s < 61 + b*5: | |
r=0 | |
elif s < 81 + b*5: | |
r=1 | |
else: | |
r=2 | |
return r | |
# author: BeeVa | |
caught_speeding = lambda s, b: int(s > 40) and int(math.ceil((s-b*5) / 20.0 - 3)) | |
### Logic-2 > close_far ### | |
# https://codingbat.com/prob/p160533 | |
# | |
# 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. | |
# | |
# close_far(1, 2, 10) → True | |
# close_far(1, 2, 3) → False | |
# close_far(4, 1, 3) → True | |
# | |
# author: BeeVa | |
def close_far(a, b, c): | |
d = (abs(b - a), abs(c - a)) | |
return (1 in d or 0 in d) and abs(b - c)+max(d) > 3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment