-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexercise_3_coding_project_2315543.py
More file actions
69 lines (52 loc) · 1.88 KB
/
exercise_3_coding_project_2315543.py
File metadata and controls
69 lines (52 loc) · 1.88 KB
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
# -*- coding: utf-8 -*-
"""Exercise_3_Coding_project_2315543.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1--BiwFzk3tYEy_Iu2B3YHD6trriGh03b
"""
def fun1(string):
# Convert the string to lowercase
string = string.lower()
# Remove spaces from the string
string = string.replace(' ', '')
# Reverse the string
reversed_string = string[::-1]
# Check if the original string matches the reversed string
return string == reversed_string
def fun2(string):
# Initialize a dictionary to store letter and digit counts
letter_digit_counts = {}
# Convert the string to lowercase
string = string.lower()
# Iterate through the string and update the dictionary with with letter/digit counts
for char in string:
if char.isalpha() or char.isdigit():
if char not in letter_digit_counts:
letter_digit_counts[char] = 0
letter_digit_counts[char] += 1
# Check if there are no letters or digits
if not letter_digit_counts:
return None
# Find the most frequent letter/digit
most_frequent_char = None
most_frequent_count = 0
for char, count in letter_digit_counts.items():
if count > most_frequent_count:
most_frequent_count = count
most_frequent_char = char
return most_frequent_char
def fun3(string):
# Initialize counters for letters, spaces, and digits
letter_count = 0
space_count = 0
digit_count = 0
# Iterate through the string and count the correponding characters
for char in string:
if char.isalpha():
letter_count += 1
elif char == ' ':
space_count += 1
elif char.isdigit():
digit_count += 1
# Return a tuple containing the three counts
return (letter_count, space_count, digit_count)