Skip to content

Commit 5fbd6ad

Browse files
committed
first commit
0 parents  commit 5fbd6ad

13 files changed

+280
-0
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Here are my solutions for coding challenges in [coderbyte](https://www.coderbyte.com)
2+
3+
Hope you have fun with these coding challenges.

coderbyte-AlphabetSoup.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
'''
2+
Have the function AlphabetSoup(str) take the str string parameter being passed and return the string with the letters in alphabetical order (ie. hello becomes ehllo). Assume numbers and punctuation symbols will not be included in the string.
3+
4+
Use the Parameter Testing feature in the box below to test your code with different arguments.
5+
'''
6+
7+
def AlphabetSoup(str):
8+
str = "".join(sorted(str))
9+
# code goes here
10+
return str
11+
12+
# keep this function call here
13+
print AlphabetSoup(raw_input())

coderbyte-CheckNums.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'''
2+
Have the function CheckNums(num1,num2) take both parameters being passed and return the string true if num2 is greater than num1, otherwise return the string false. If the parameter values are equal to each other then return the string -1.
3+
4+
Use the Parameter Testing feature in the box below to test your code with different arguments.
5+
'''
6+
7+
def CheckNums(num1,num2):
8+
if num2 > num1:
9+
return 'true'
10+
elif num2 < num1:
11+
return 'false'
12+
else:
13+
return '-1'
14+
15+
# keep this function call here
16+
print CheckNums(raw_input())

coderbyte-ChessboardTraveling.py

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
'''
2+
Challenge (hard)
3+
4+
Have the function ChessboardTraveling(str) read str which will be a string consisting of the location of a space on a standard 8x8 chess board with no pieces on the board along with another space on the chess board. The structure of str will be the following: "(x y)(a b)" where (x y) represents the position you are currently on with x and y ranging from 1 to 8 and (a b) represents some other space on the chess board with a and b also ranging from 1 to 8 where a > x and b > y. Your program should determine how many ways there are of traveling from (x y) on the board to (a b) moving only up and to the right. For example: if str is (1 1)(2 2) then your program should output 2 because there are only two possible ways to travel from space (1 1) on a chessboard to space (2 2) while making only moves up and to the right.
5+
6+
Hard challenges are worth 15 points and you are not timed for them.
7+
Sample Test Cases
8+
9+
Input:"(1 1)(3 3)"
10+
11+
Output:6
12+
13+
14+
Input:"(2 2)(4 3)"
15+
16+
Output:3
17+
18+
'''
19+
20+
def ChessboardTraveling(str):
21+
x, y, a, b = (int(str[1]), int(str[3]), int(str[6]), int(str[8]))
22+
rightSteps = a - x
23+
upSteps = b - y
24+
totalSteps = rightSteps + upSteps
25+
totalSteps_permutation = permutation(totalSteps)
26+
upSteps_permutation = permutation(upSteps)
27+
rightSteps_permutation = permutation(rightSteps)
28+
posibleSteps = int(totalSteps_permutation / (upSteps_permutation * rightSteps_permutation))
29+
return posibleSteps
30+
31+
def permutation(num):
32+
result = 1
33+
for i in range(1, num+1):
34+
result *= i
35+
return result
36+
37+
# keep this function call here
38+
print ChessboardTraveling(raw_input())

coderbyte-FirstFactorial.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
'''
2+
Have the function FirstFactorial(num) take the num parameter being passed and return the factorial of it. For example: if num = 4, then your program should return (4 * 3 * 2 * 1) = 24. For the test cases, the range will be between 1 and 18 and the input will always be an integer.
3+
4+
Use the Parameter Testing feature in the box below to test your code with different arguments.
5+
'''
6+
7+
def FirstFactorial(num):
8+
if num == 1:
9+
return 1
10+
else:
11+
return num * FirstFactorial(num - 1)
12+
13+
str = input()
14+
print(FirstFactorial(str))

coderbyte-FirstReverse.py

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
'''
2+
Challenge
3+
Have the function FirstReverse(str) take the str parameter being passed and return the string in reversed order. For example: if the input string is "Hello World and Coders" then your program should return the string sredoC dna dlroW olleH.
4+
Sample Test Cases
5+
6+
Input:"coderbyte"
7+
8+
Output:etybredoc
9+
10+
11+
Input:"I Love Code"
12+
13+
Output:edoC evoL I
14+
'''
15+
16+
def FirstReverse(str):
17+
lenStr = len(str)
18+
revert = ""
19+
for i in range(lenStr):
20+
revert = revert + str[lenStr-i-1]
21+
# code goes here
22+
str = revert
23+
return str
24+
25+
# keep this function call here
26+
str = input()
27+
print(FirstReverse(str)

coderbyte-KaprekarsConstant.py

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
'''
2+
Challenge(hard)
3+
4+
Have the function KaprekarsConstant(num) take the num parameter being passed which will be a 4-digit number with at least two distinct digits. Your program should perform the following routine on the number: Arrange the digits in descending order and in ascending order (adding zeroes to fit it to a 4-digit number), and subtract the smaller number from the bigger number. Then repeat the previous step. Performing this routine will always cause you to reach a fixed number: 6174. Then performing the routine on 6174 will always give you 6174 (7641 - 1467 = 6174). Your program should return the number of times this routine must be performed until 6174 is reached. For example: if num is 3524 your program should return 3 because of the following steps: (1) 5432 - 2345 = 3087, (2) 8730 - 0378 = 8352, (3) 8532 - 2358 = 6174.
5+
6+
Hard challenges are worth 15 points and you are not timed for them.
7+
Sample Test Cases
8+
9+
Input:2111
10+
11+
Output:5
12+
13+
14+
Input:9831
15+
16+
Output:7
17+
18+
'''
19+
20+
count = 0
21+
22+
def KaprekarsConstant(num):
23+
global count
24+
if num == '6174':
25+
return count
26+
else:
27+
num1 = "".join(sorted(str(num))) #ascending order
28+
num2 = "".join(sorted(str(num), reverse=True)) #descending order
29+
num1 = int(num1)
30+
num2 = int(num2)
31+
subtractResult = abs(num1 - num2)
32+
diff = str(subtractResult)
33+
diffLength = len(diff)
34+
if diffLength < 4:
35+
for i in range(0, 4-diffLength):
36+
diff = '0' + diff
37+
count += 1
38+
return KaprekarsConstant(str(diff))
39+
40+
# keep this function call here
41+
print KaprekarsConstant(raw_input())

coderbyte-LetterCapitalize.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'''
2+
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.
3+
4+
Use the Parameter Testing feature in the box below to test your code with different arguments.
5+
'''
6+
7+
def LetterCapitalize(str):
8+
9+
list = str.split(" ")
10+
newStr = ""
11+
for word in list:
12+
newStr += word[0].upper()
13+
for i in range(1, len(word)):
14+
newStr += word[i]
15+
newStr += " "
16+
newStr = newStr[:-1]
17+
return newStr
18+
19+
# keep this function call here
20+
print LetterCapitalize(raw_input())

coderbyte-LetterChanges.py

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
### coderbyte
2+
3+
'''
4+
Challenge
5+
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.
6+
Sample Test Cases
7+
8+
9+
Input:"hello*3"
10+
11+
Output:Ifmmp*3
12+
13+
14+
Input:"fun times!"
15+
16+
Output:gvO Ujnft!
17+
18+
'''
19+
20+
def LetterChanges(str):
21+
letterList = "abcdefghijklmnopqrstuvwxyz"
22+
vowelList = "aeiou"
23+
str = str.lower()
24+
newStr = ""
25+
26+
# to the next letter
27+
for i in range(len(str)):
28+
if str[i].isalpha():
29+
index = letterList.find(str[i])
30+
letter = letterList[index+1]
31+
if vowelList.find(letter) != -1:
32+
letter = letter.upper()
33+
newStr = newStr + letter
34+
else:
35+
newStr = newStr + str[i]
36+
37+
str = newStr
38+
return str
39+
40+
# keep this function call here
41+
str = input()
42+
#letterList = "abcdefghijklmnopqrstuvwxyz"
43+
#print(letterList.find("a"))
44+
print(LetterChanges(str))

coderbyte-LongestWord.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'''
2+
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.
3+
4+
Use the Parameter Testing feature in the box below to test your code with different arguments.
5+
'''
6+
7+
import re
8+
9+
def LongestWord(sen):
10+
list = sen.split(" ")
11+
longest = re.sub(r'[^a-zA-Z0-9]',"", list[0])
12+
for word in list:
13+
word = re.sub(r'[^a-zA-Z0-9]',"", word)
14+
if len(word) > len(longest):
15+
longest = word
16+
return longest
17+
18+
# keep this function call here
19+
str = input()
20+
print(LongestWord(str))

coderbyte-SimpleAdding.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
'''
2+
Have the function SimpleAdding(num) add up all the numbers from 1 to num. For example: if the input is 4 then your program should return 10 because 1 + 2 + 3 + 4 = 10. For the test cases, the parameter num will be any number from 1 to 1000.
3+
4+
Use the Parameter Testing feature in the box below to test your code with different arguments.
5+
'''
6+
7+
def SimpleAdding(num):
8+
sum = num
9+
for i in range(num):
10+
sum += i
11+
return sum
12+
13+
# keep this function call here
14+
print SimpleAdding(raw_input())

coderbyte-SimpleSymbols.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'''
2+
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.
3+
4+
Use the Parameter Testing feature in the box below to test your code with different arguments.
5+
'''
6+
7+
def SimpleSymbols(str):
8+
str = '=' + str + '='
9+
for c in str:
10+
if c.isalpha():
11+
if not str[str.index(c)-1] == '+' or not str[str.index(c)+1] == '+':
12+
return 'false'
13+
return 'true'
14+
15+
# keep this function call here
16+
print SimpleSymbols(raw_input())

coderbyte-TimeConvert.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
'''
2+
Have the function TimeConvert(num) take the num parameter being passed and return the number of hours and minutes the parameter converts to (ie. if num = 63 then the output should be 1:3). Separate the number of hours and minutes with a colon.
3+
4+
Use the Parameter Testing feature in the box below to test your code with different arguments.
5+
'''
6+
7+
def TimeConvert(num):
8+
hour = int(num/60)
9+
min = num - hour * 60
10+
# code goes here
11+
return str(hour) + ':' + str(min)
12+
13+
# keep this function call here
14+
print TimeConvert(raw_input())

0 commit comments

Comments
 (0)