/
CS111_codesignal.py
107 lines (83 loc) · 3.01 KB
/
CS111_codesignal.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
'''Create a function that returns True if the given string has any of the following:
Only letters and no numbers.
Only numbers and no letters.
If a string has both numbers and letters or contains characters that don't fit into any category, return False.
Examples:
csAlphanumericRestriction("Bold") ➞ True
csAlphanumericRestriction("123454321") ➞ True
csAlphanumericRestriction("H3LL0") ➞ False'''
def csAlphanumericRestriction(input_str):
if input_str.isdigit():
return bool(1)
elif input_str.isalpha():
return bool(1)
else:
return bool(0)
'''Codesignal solution'''
def csAlphanumericRestriction(input_str):
return input_str.isnumeric() or input_str.isalpha()
'''Jimmy Slagle solution'''
def csAlphanumericRestriction(input_str):
return bool(re.fullmatch("[a-zA-Z]+|[\d]+", input_str))
'''Write a function that takes a string as input and returns that string in reverse order, with the opposite casing for each character within the string.
Examples:
csOppositeReverse("Hello World") ➞ "DLROw OLLEh"
csOppositeReverse("ReVeRsE") ➞ "eSrEvEr"
csOppositeReverse("Radar") ➞ "RADAr"'''
def csOppositeReverse(txt):
txt = txt[::-1]
reversed = []
for letter in txt:
if letter.isupper():
reversed.append(letter.lower())
elif letter.islower():
reversed.append(letter.upper())
else:
reversed.append(" ")
# elif letter == " ":
# reversed.append(" ")
joined = "".join(reversed)
return joined
'''Codesignal solution'''
def csOppositeReverse(txt):
return txt.swapcase()[::-1]
'''Jimmy solution'''
import string
def csOppositeReverse(txt):
lower = string.ascii_lowercase
upper = string.ascii_uppercase
before = lower + upper
after = upper + lower
code = txt.maketrans(before, after)
encoded = txt.translate(code)
return encoded[::-1]
'''Create a function that given an integer, returns an integer where every digit in the input integer is squared.
Examples:
csSquareAllDigits(9119) -> 811181 because 9^2 = 81, 1^2 = 1, 1^2 = 1, and 9^2 = 81
csSquareAllDigits(2483) -> 416649 because 2^2 = 4, 4^2 = 16, 8^2 = 64, and 3^2 = 9'''
def csSquareAllDigits(n):
n_string = str(n)
n_list = list(n_string)
n_squared = []
for elem in n_list:
elem = int(elem)
elem = elem**2
n_squared.append(elem)
n_squared_str = [str(n) for n in n_squared]
joined_str = "".join(n_squared_str)
joined_int = int(joined_str)
return joined_int
'''Codesignal solution'''
def csSquareAllDigits(n):
return int("".join(str(int(i)**2) for i in str(n)))
'''Given a string, return a new string with all the vowels removed.
Examples:
csRemoveTheVowels("Lambda School is awesome!") -> "Lmbd Schl s wsm!"
Notes:
For this challenge, "y" is not considered a vowel.'''
def csRemoveTheVowels(input_str):
x = re.sub(r'[aeiouAEIOU]', "", input_str)
return x
'''Codesignal solution'''
def csRemoveTheVowels(input_str):
return "".join(c for c in input_str if c.lower() not in "aeiou")