# AI News, Difference between revisions of "How to Think Like a Computer Scientist: Learning with Python 2nd Edition/Solutions"

## Difference between revisions of "How to Think Like a Computer Scientist: Learning with Python 2nd Edition/Solutions"

The following section contains answers to the exercises in the Book.

Why does a call to num_digits(-24) result in an infinite loop (hint: -1/10 evaluates to -1)?

The loop will end when n = 0, and per above -1/10 == -1, causing an infinite loop.

Encapsulate: in a function named count_letters, and generalize it so that it accepts the string and the letter as arguments.

Now rewrite the count_letters function so that instead of traversing the string, it repeatedly calls find (the version from Optional parameters), with the optional third parameter to locate new occurences of the letter being counted.

ceil function finds the lowest integeral value greater than or equal to x.

deepcopy would have come handy in exercises you didn't have to solve regarding object reference, thus no answer is excpected here.

## Given a string, find its first non-repeating character

Given a string, find the first non-repeating character in it.

Example: Implementation:C/C++// C program to find first non-repeating character #include&lt;stdlib.h> #include&lt;stdio.h> #define NO_OF_CHARS 256 /* Returns an array of size 256 containg count of characters in the passed char array */ int

i++) { if (count[*(str+i)] == 1) { index = i;

} /* The method returns index of first non-repeating character in a string.

i++) { if (count[str.charAt(i)] == 1) { index = i;

} // Driver method public static void main (String[] args) { String str = 'geeksforgeeks';

'Either all characters are repeating or string ' + 'is empty' : 'First non-repeating character is ' +

of characters in the passed char array def getCharCountArray(string): count = [0] * NO_OF_CHARS for i in string: count[ord(i)]+=1return count # The function returns index of first non-repeating #

then returns -1 def firstNonRepeating(string): count = getCharCountArray(string) index = -1 k

i in string: if count[ord(i)] == 1: index = k breakk += 1 return

index # Driver program to test above function string = 'geeksforgeeks' index

print 'First non-repeating character is ' + string[index] # This code is contributed by Bhavya Jain Run on IDE C# //

} /* The method returns index of first non-repeating character in a string.

i++) { if (count[str[i]] == 1) { index = i;

} // Driver code public static void Main() { string str = 'geeksforgeeks';

'Either ' + 'all characters are repeating or string ' + 'is empty' : 'First non-repeating character' +

\$i++) { if (\$count[ord(\$str[\$i])] == 1) { \$index = \$i;

The first part of the algorithm runs through the string to construct the count array (in O(n) time).

can augment the count array by storing not just counts but also the index of the first time you encountered the character e.g.

## Return maximum occurring character in an input string

Write an efficient function to return maximum occurring character in the input string e.g., if input string is “test” then function should return ‘t’.

Algorithm:One obvious approach to solve this problem would be to sort the input string and then traverse through the sorted string to find the character which is occurring maximum number of times.

But if we know that our input string will have characters with value from 0 to 127 only, we can limit Hash array size as 128.

Notes:If more than one characters have the same count and that count is maximum then the function returns the first character with maximum count in input string.

Python Find String Method

Be sure to like, share and comment to show your support for our tutorials. ======================================= Channel - ..

Excel: Find the Position of the First Letter in a Text String

The video explains how to use Excel to find the position of the alpha character in a text string. The formula generated in the video is: {=MATCH(TRUE ...

How to find index of a character or string in java?

get character index string java, find index character string java, character index string java, character at index string java, get character at index of string java, ...

Find Text in String With Excel SEARCH Function

The SEARCH function looks for a text string, within another text string. The SEARCH function ..

IF Function with Partial Match on Text String or "Contains" Criteria

This video demonstrates how to test for a partial match on a text string in the context of an IF function. The solution uses the SEARCH and ISNUMBER functions ...

IF Function with Multiple Partial Match Text String Criteria

This video demonstrates how to specify multiple partial text string match criteria in a logical test. For example does text contain X and Y. The solution uses the ...

Return maximum occurring character in an input string | GeeksforGeeks

Find Complete Code at GeeksforGeeks Article: Practice Problem Online ..

Microsoft Access Tutorial: String Functions (Left, Right, InStr, Trim)

In this Microsoft Access tutorial, I'll show you how to break up text fields into smaller parts using Left, Right, Trim, and InStr.

Excel Magic Trick 699: VLOOKUP Mixed Number & Letter Partial Text Extract For Lookup Value

Complex text extract formula that extracts text to use in VLOOKUP to lookup values and return data. See these Tricks: 1. Extract all letters from text string 2.

How to find the most repeated character in a string in C#.NET

In this video, I will show you how to use Visual Studio to write a C#.NET program to display the most repeated character in a string using a fast O(n) technique.