Programming Assignment - 9
Programming Assignment - Week 9
Note: There could be optimised ways to solve the problem, but the goal here is to solve it effectively. The most intuitive solution has been provided for clarity.
Programming Assignment 1
Question:
Write a Python function that counts the number of vowels in a given string s
. The function should return the number of vowels found, considering both uppercase and lowercase vowels.
Input Format:
- The input consists of a single string
s
.
Output Format:
- A single integer representing the count of vowels in the string.
Example:
Input: hello world Output: 3
Code:
def count_vowels(s):
vowels = {'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'}
vowel_count = 0
for char in s:
if char in vowels:
vowel_count += 1
return vowel_count
s = input()
print(count_vowels(s), end="")
Programming Assignment 2
Question:
Write a Python function that takes a string and determines if the average length of the words in the string is greater than 4. If the average word length is greater than 4, the function should return 1
; otherwise, it should return 0
.
Input Format:
- The input consists of a single string containing words separated by spaces.
Output Format:
- A single integer:
1
if the average word length is greater than 4,0
otherwise.
Example:
Input: The quick brown fox jumps over the lazy dog Output: 0
Code:
def average_word_length_greater_than_4(s):
words = s.split()
if len(words) == 0:
return 0
total_length = sum(len(word) for word in words)
average_length = total_length / len(words)
return 1 if average_length > 4 else 0
input_string = input()
print(average_word_length_greater_than_4(input_string), end="")
Programming Assignment 3
Question:
Write a Python function that takes an integer n
representing the number of nodes in a complete graph and returns the total number of edges in the graph.
A complete graph is a graph in which every pair of distinct nodes is connected by a unique edge.
Input Format:
- The input consists of a single integer
n
which represents the number of nodes in the complete graph.
Output Format:
- A single integer representing the total number of edges in the complete graph of
n
nodes.
Example:
Input: 4 Output: 6
Code:
def total_edges_in_complete_graph(n):
return n * (n - 1) // 2
n = int(input())
print(total_edges_in_complete_graph(n), end="")
Comments
Post a Comment