-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrials.py
124 lines (76 loc) · 2.06 KB
/
trials.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
"""Python functions for JavaScript Trials 1."""
def output_all_items(items):
for item in items:
print(item)
def get_all_evens(nums):
even_nums = []
for num in nums:
if num % 2 == 0:
even_nums.append(num)
return even_nums
def get_odd_indices(items):
result = []
for idx in range(len(items)):
if idx % 2 == 0:
result.append(items[idx])
return result
def print_as_numbered_list(items):
i = 1
for item in items:
print(f'{i}. {item}')
i += 1
def get_range(start, stop):
nums = []
for num in range(start, stop):
nums.append(num)
return nums
def censor_vowels(word):
chars = []
for letter in word:
if letter in 'aeiou':
chars.append('*')
chars.append(letter)
return ''.join(chars)
def snake_to_camel(string):
camel_case = []
for word in string.split('_'):
camel_case.append(f'{word[0].upper()}{word[1:]}')
return ''.join(camel_case)
def longest_word_length(words):
longest = len(words[0])
for word in words:
if longest < len(word):
longest = len(word)
return longest
def truncate(string):
result = []
for char in string:
if len(result) == 0 or char != result[-1]:
result.append(char)
return ''.join(result)
def compress(string):
compressed = []
curr_char = ''
char_count = 0
for char in string:
if char != curr_char:
compressed.append(curr_char)
if char_count > 1:
compressed.append(str(char_count))
curr_char = char
char_count = 0
char_count += 1
compressed.append(curr_char)
if char_count > 1:
compressed.append(str(char_count))
return ''.join(compressed)
def has_balanced_parens(string):
parens = 0
for char in string:
if char == '(':
parens += 1
elif char == ')':
parens -= 1
if parens > 0:
return False
return parens < 0