Skip to content

Instantly share code, notes, and snippets.

@zemadi
Last active April 16, 2024 21:30
Show Gist options
  • Save zemadi/11071837 to your computer and use it in GitHub Desktop.
Save zemadi/11071837 to your computer and use it in GitHub Desktop.
Python answers to CoderByte challenges
#My solutions to challenges from CoderByte.com. Their Python interpreter is buggy.
#1 Using the Python language, have the function FirstReverse(str) take the str parameter being passed and return the string in reversed order.
def FirstReverse(str):
print str[::-1]
#2 Using the Python language, have the function FirstFactorial(num) take the num parameter being passed and return the factorial of it (ie. if num = 4, return (4 * 3 * 2 * 1)). For the test cases, the range will be between 1 and 18.
def FirstFactorial(num):
sum = 1
count = 1
while count <= num:
sum = sum * count
count += 1
print sum
#3 Using the Python language, have the function LongestWord(sen) take the sen parameter being passed and return the largest word in the string. If there are two or more words that are the same length, return the first word from the string with that length. Ignore punctuation and assume sen will not be empty.
def LongestWord(sen):
test = sen.split(" ")
greatest = test[0]
for word in test:
if len(word) > len(greatest):
greatest = word
print greatest
#4: Using the Python language, have the function LetterChanges(str) take the str parameter being passed and modify it using the following algorithm. Replace every letter in the string with the letter following it in the alphabet (ie. c becomes d, z becomes a). Then capitalize every vowel in this new string (a, e, i, o, u) and finally return this modified string.
def LetterChanges(str):
lower = str.lower()
newstring = ""
lookup = "abcdefghijklmnopqrstuvwxyz"
vowels = "aeiou"
for letter in lower:
if letter == "z":
newstring += "A"
else:
letter_index = lookup.index(letter)
if lookup[(letter_index+1)] in vowels:
vowel_set = lookup[(letter_index+1)]
newstring += vowel_set.upper()
else:
newstring += lookup[(letter_index+1)]
print newstring
#5: Using the Python language, have the function SimpleAdding(num) add up all the numbers from 1 to num. For the test cases, the parameter num will be any number from 1 to 1000.
def SimpleAdding(num):
total = 0
for number in range(1, int(num)):
total += number
print total
#6: Using the Python language, have the function LetterCapitalize(str) take the str parameter being passed and capitalize the first letter of each word. Words will be separated by only one space.
def LetterCapitalize(str):
print str.title()
#or, for the long way
test = []
for word in str.split():
test.append(word.title())
new_str = ' '.join(test)
print new_str
# 7: Using the Python language, have the function SimpleSymbols(str) take the str parameter being passed and determine if it is an acceptable sequence by either returning the string true or false. The str parameter will be composed of + and = symbols with several letters between them (ie. ++d+===+c++==a) and for the string to be true each letter must be surrounded by a + symbol. So the string to the left would be false. The string will not be empty and will have at least one letter.
# Example input: str = "+d+=3=+s+"
def SimpleSymbols(str):
# Tried regex (/(\+[A-Za-z]\+)+/g) but it wouldn't work
lookup = "acbdefghijklmnopqrstuvwxyz"
lower = str.lower()
test = []
for i in range(0, len(lower)):
if lower[i] in lookup:
if lower[i-1] == '+' and lower[i+1] == "+":
test.append(True)
else:
test.append(False)
if False not in test:
print "true"
else:
print "false"
'''
At this point, skipped ahead to medium difficulty.
Medium difficulty question 1: Using the Python language, have the function PrimeTime(num) take the num parameter being passed and return the string true if the parameter is a prime number, otherwise return the string false. The range will be between 1 and 2^16.
'''
def PrimeTime(num):
# Create an empty variable which will be used to count the number of times a number divides into the number.
test = 0
if num % 2 == 0:
print "false"
else:
sqrt = int(num**(0.5))
# We want the range to include the square root but not go above it, so have to add 1. You could also round it but CoderByte does not allow library imports.
for i in range(2, sqrt+1):
if num % i == 0:
test += 1
if test == 0:
print "true"
else:
print "false"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment