Question

Python Code Exercise #3 name: count_lead_letter arguments: * s (str): s will all lowercase letter with...

Python Code

Exercise #3

name:

count_lead_letter

arguments:

* s (str): s will all lowercase letter with no puntuation

returns:

A dictionary keyed by a letter and it's value is a count of the number of times that letter is the first character in a word in the string s

note:

Each word is seperated by a single space
0 0
Add a comment Improve this question Transcribed image text
Answer #1

CODE:

#function definition of count_lead_letter
def count_lead_letter(s):
#Declaring empty dictionary
d = dict()
#Splitting given string into words
#based on space between them
#And it will store all these words
#in a list s
s = s.split(' ')
#loop will iterate through the words
#the list of words
for i in s:
#Here i is the word in the string
#i[0] is te first character
#if the character not present in dictionary
if i[0] not in d:
# then set its value to 1
d[i[0]]=1
#if the character already present in dictionary
else:
#then increments it by 1
d[i[0]]+=1
#returnn dictionary d
return d

#Prompt user for the string
st = input("Enter String: ")
#Calling the function by pasing string ans store it in d
d = count_lead_letter(st)
#printig the dictionary d
print(d)

CODE SCREENSHOT:

#function definition of count lead letter def count lead letter(s): #Declaring empty dictionary d = dict() #Splitting given s

OUTPUT:

LIUIIOIIUL L LLL L Enter String: hai hello this is to test the output only {h: 2, t: 4, i: 1, o: 2} >>>

Please make sure of indentation of the code(as shown in screenshot)

Here first we split the string into words by space using split method

and then we iterate through each word in list and check for whether the first character is present in dictionary or not

if it is not present then make its value as 1 with key as that letter

if it already present then increment the key's value to 1

then return the dictionary.

Please Don't forget to UPVOTE my solution and comment if you have any queries. I will defintely answer.

Add a comment
Know the answer?
Add Answer to:
Python Code Exercise #3 name: count_lead_letter arguments: * s (str): s will all lowercase letter with...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • IN PYTHON 3: Given a sentence, s, produce a dictionary that tells you the number of...

    IN PYTHON 3: Given a sentence, s, produce a dictionary that tells you the number of times each word occurs in a string. You may assume each word is lowercase, there are no contractions, and each word is separated by a space. Please note, your function must return the created dictionary. please test that it works with this test : wordFrequency("was it a car or cat i saw or was it a kangaroo")

  • Write a Python function called more() that takes three string inputs and outputs a string Formally,...

    Write a Python function called more() that takes three string inputs and outputs a string Formally, the function signature and output are given by rucharist, char: str words str) > str Use the same names for the input arguments as shown above Note that charl and char2 will always be a string of length 1 (ie, it is a single character. The function checks which of charl or char2 appears more often in the word string and returns that character...

  • Python code that Returns True if every letter of the (lowercase) alphabet can be found (at...

    Python code that Returns True if every letter of the (lowercase) alphabet can be found (at least once) in s, False otherwise

  • Using Python: #Write a letterAppearance function that takes a string, str as argument #letterAppearance will count...

    Using Python: #Write a letterAppearance function that takes a string, str as argument #letterAppearance will count the number of times each letter appears in a string #using a dictionary. #letterAppearance returns only the letters that appear in the string. #Capitalization should not matter ''' letterAppearance('hello') #{'h': 1, 'e': 1, 'l': 2, 'o': 1} letterAppearance('Hello') #{'h': 1, 'e': 1, 'l': 2, 'o': 1} letterAppearance('mutation') #{'m': 1, 'u': 1, 't': 2, 'a': 1, 'i': 1, 'o': 1, 'n': 1} '''

  • Python 3: Write a function CharCount, to take a string S and a single character C,...

    Python 3: Write a function CharCount, to take a string S and a single character C, and returns the count of C (one letter) in the string S. If the letter of C is not found in S, the function returns -1. You must write your own function to do this and can not use any built-in counting functions. Use the function in main to get two inputs from the user, a sentence and a character, then show the count...

  • Question 2 - Objects You are to implement the following functions: getCharacterFrequency -10pts This function takes...

    Question 2 - Objects You are to implement the following functions: getCharacterFrequency -10pts This function takes a single string (str) argument and returns an object. o The object's properties will be the unique letters present in str The value of each property will be the frequency of each character present in the string. NOTE: Uppercase and Lowercase letters should be grouped in the same count. For example, the word Babble would result in the following object "B3, 11 This count...

  • Python Language Create a module (1 mark) a) Create a function that takes three (3) arguments...

    Python Language Create a module (1 mark) a) Create a function that takes three (3) arguments (1 mark) i) Name it whatever you’d like (1 mark) ii) It will return a dictionary with the following keys (1 mark) (1) Error => gives error message or empty string if not (1 mark) (2) Result => gives result or empty string if no result (1 mark) b) Ensure that all three arguments are of String (str) datatype (1 mark) i) Return an...

  • In python Exercise 4 (graded) Write a function "passValidate" that takes a string "s" and checks...

    In python Exercise 4 (graded) Write a function "passValidate" that takes a string "s" and checks the validity of string as a password Validation Rules: At least 1 lowercase letter between [a-z] and 1 upper case letter between [A-Z]. At least 1 number between [0-9 At least 1 character from [$#@]. Minimum length 6 characters. Maximum length 16 characters. CS103-Lab 13 Page 3 of 3 Sample Input: s"Uab@2762" Sample Output: True

  • Python code define a function car that takes 'swift' as a parameter function should count how...

    Python code define a function car that takes 'swift' as a parameter function should count how many letters in the string function should return a dictionary where key is lower case alpha caracter and value is the number of times that character appears in sentence. ex- input- that big car is so expensive output- 't':1, 'h':1, 'a':2, 'b':1, 'i':3,......

  • Please write code in C++ and include all headers not bits/stdc: 17.10 (Occurrences of a specified...

    Please write code in C++ and include all headers not bits/stdc: 17.10 (Occurrences of a specified character in a string) Write a recursive function that finds the number of occurrences of a specified letter in a string using the following function header. int count(const string& s, char a) For example, count("Welcome", 'e') returns 2. Write a test program that prompts the user to enter a string and a character, and displays the number of occurrences for the character in the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT