forked from calistus-igwilo/python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test2.py
131 lines (103 loc) · 2.94 KB
/
test2.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
125
126
127
128
129
130
131
from curses.ascii import islower
from itertools import combinations, permutations
x = [1, 2, 3, 4]
y = [5, 6]
x.append(y)
print(x)
"""
Rotate an array to the right by k elements
"""
def rotate_array(arr, k):
end = len(arr)-1
if k <= 0:
return arr
for i in range(0, k, 1):
#a, b = arr[0], arr[end]
# arr = [arr[end]] + [arr[0]] + arr[1:end]
arr = [arr[end]] + arr[0:end]
return arr
print(rotate_array([1, 2, 3, 4, 5], 4))
text = """Python is powerful... and fast
plays well with others
runs everywhere
is friendly & easy to learn
is Open
These are some of the reasons people who use Python would rather not use anything else"""
# words = text.lower().strip().replace('.', '').split()
# result = []
# for word in words:
# if len(word) > 6:
# result.append(word)
# print(result)
# Another solution using list comprehension
words = text.lower().strip().replace('.', '').replace(',', '').split()
words = [word for word in words if len(word) > 6]
print(words)
###############################
x=1; y=1; z=2; n=3
result = [[a,b,c] for a in range(x+1) for b in range(y+1) for c in range(z+1) if (a+b+c) != n]
print(result)
################################
def score_words(words):
str = words.split()
score = 0
for word in str:
if len(word)%2 == 0:
score+=2
print(score)
score_words("programming is awesome")
#################################
# All substrings of a string and calculate how many that starts with vowels and consonants
def minion_game(string_):
vowel_strings = 0
consonant_strings = 0
string_length = len(string_) # length of string
for i in range(string_length):
if string_[i] in "AEIOU":
vowel_strings += string_length - i
else:
consonant_strings += string_length -i
# Determine the winner
if consonant_strings > vowel_strings:
print("Stuart", consonant_strings )
elif consonant_strings < vowel_strings:
print("Kevin", vowel_strings)
else:
print("Draw")
minion_game("BANANA")
def merge_the_tools(string, k):
sub_sequences = []
temp = ""
for i in range(0, len(string), k):
sub_sequences.append((string[i:i+k]))
print(sub_sequences)
print("lenght of subsequence: ",len(sub_sequences))
# check for duplicate alphabets
for string in sub_sequences:
for i in range(k):
if string[i] not in temp:
temp += string[i]
print(temp)
temp = ""
s = 'AAABCADDE'
merge_the_tools(s, 3)
def swap_case(s):
result = ""
for i in range(len(s)):
if s[i].isupper():
s = s.replace(s[i], s[i].upper())
else:
s = s.replace(s[i], s[i].lower())
print(s)
return s
t = "Www.HackerRank.com"
swap_case(t)
B = "baNM.y"
C = "caLY"
d = ""
if B[1].islower():
d += B[1].upper()
print(d)
print(B[4].isupper())
print(B[1].upper())
print(C[2].isupper())