Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit cd4ee2c

Browse files
HCL Interview program
1 parent ed1861d commit cd4ee2c

File tree

4 files changed

+80
-0
lines changed

4 files changed

+80
-0
lines changed

‎Interview Questions/text_counter.py‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
def frequent_count_text(text):
2+
dict_count = {}
3+
for i in text.split():
4+
if i.lower() in dict_count:
5+
dict_count[i.lower()] += 1
6+
else:
7+
dict_count[i.lower()] = 1
8+
return dict_count
9+
10+
# Example usage:
11+
input_text = "This is a sample text with several words this is a test text"
12+
result = frequent_count_text(input_text)
13+
print(result)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Repeated words and letter counter and display top num letters and word their counts and remove punctuation
2+
3+
4+
def frequent_count_text(text, num=3):
5+
word_count = {}
6+
text_count = {}
7+
for i in text.split():
8+
if i.lower() in word_count:
9+
word_count[i.lower()] += 1
10+
else:
11+
word_count[i.lower()] = 1
12+
for j in i:
13+
if j.lower() in text_count:
14+
text_count[j.lower()] += 1
15+
else:
16+
text_count[j.lower()] = 1
17+
top_words = sorted(word_count.items(), key=lambda kv: (-kv[1], kv[0]))[:num]
18+
top_letters = sorted(text_count.items(), key=lambda kv: (-kv[1], kv[0]))[:num]
19+
# 4) Format outputs and return (letters first, words second)
20+
letter_lines = [f"Top {num} letter:"]
21+
letter_lines += [f"{ch}: {cnt}" for ch, cnt in top_letters]
22+
23+
word_lines = [f"Top {num} word:
24+
"]
25+
word_lines += [f"{w}: {cnt}" for w, cnt in top_words]
26+
27+
letter_output = "\n".join(letter_lines)
28+
word_output = "\n".join(word_lines)
29+
return f"{letter_output}\n\n{word_output}"
30+
31+
32+
# Example usage:
33+
input_text = "This is a sample text with several words this is a test text"
34+
result = frequent_count_text(input_text, 3)
35+
print(result)

‎Interview Questions/vowels.py‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Vowels:
2+
def __init__(self, data):
3+
self.data = data
4+
self.vowels = ["a", "e", "i", "o", "u"]
5+
6+
def filter_non_vowels(self):
7+
result = "".join(
8+
[char for char in self.data if char.lower() not in self.vowels]
9+
)
10+
return result
11+
12+
def filter_vowels(self):
13+
result = "".join([char for char in self.data if char.lower() in self.vowels])
14+
return result
15+
16+
17+
# Example usage:
18+
input_string = "Hello World"
19+
vowel_filter = Vowels(input_string)
20+
print("String after removing vowels:", vowel_filter.filter_non_vowels())
21+
print("String with only vowels:", vowel_filter.filter_vowels())

‎hackerrank/swapcase.py‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Swap case of each character in the string
2+
def swap_case(s):
3+
data = []
4+
for i in s:
5+
if "a" <= i <= "z":
6+
data.append(chr(ord(i) - 32))
7+
elif "A" <= i <= "Z":
8+
data.append(chr(ord(i) + 32))
9+
else:
10+
data.append(i)
11+
return "".join(data)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /