![]() This solution uses a for loop (lines 5 to 8) to iterate through msg.įor each character in msg, we apply the count() function to get the number of occurrences for that character. To test your function, you can use the statements below: If there is more than one character that occurs most frequently, the function returns the first character.įor instance, if msg equals 'abbbccddd', the function returns the tuple ('b', 3). Hence, the function should return the tuple ('b', 3). This function is case sensitive and returns a tuple with the following information – the most frequently occurring character in msg (excluding spaces) and the number of times the character occurs.įor instance, if msg equals 'abbbcc', the most frequently occurring character is 'b' and it appeared three times. ![]() Today, our task is to write a function called mostFreqChar() that has one parameter – msg. Now that we are familiar with the count() function, let’s work on the practice question for today. Hence, the first and last 'h' are omitted. Hence, the first 'h' at position 3 is omitted.įinally, we get 1 for the last example as we ask the count() function to count from positions 5 to 9. Next, we get 2 for the second example as this time, we ask the count() function to start counting from position 5. ![]() We get 3 for the first example as 'h' appeared three times in msg, at positions 3, 6 and 10. This function accepts an argument – the item to count – and returns the number of times the item appears in the list. Let’s start with the count() function for lists. The function behaves a bit differently for lists vs strings. The count() function is a built-in function in Python that returns the number of times a certain element appears in a list or string.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |