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.

The word 'alice' occurs 386 times (not including 12 occurences of 'alice's')

Testing String Characteristics

NOTE: The deliberate decision in OpenRefine is that using toString on null results in the string 'null' When you see an empty cell in an OpenRefine project there are broadly three possibilities: In OpenRefine 3.0 upwards we've introduced some more options for handling the difference between null and zero length strings, although there are probably more improvements to be made, and we probably can't get it right for everyone at the same time.

For a simple example, replace('The cow jumps over the moon and moos', 'oo', 'ee') returns the string The cow jumps over the meen and mees.

Another match() example: isNotNull(value.match(/(a.c)/)) returns True or False if value is like abc or azc , etc but would not match a value of ac.

Example: Cell contains value: hello 123456 goodbye value.match(/\d{6}/) = null value.match(/.*\d{6}.*/) = [ ] (empty array) value.match(/.*(\d{6}).*/) = [ '123456' ] (array with one captured value) value.match(/(.*)(\d{6})(.*)/) = [ 'hello ', '123456', ' goodbye' ] (array with three captured values) More info on Regex And a nice online learning &

Example of use: value = abeadsabmoloei value.find(/[aeio]+/) = ['a','ea','a','o','oei'] This is the equivalent of the following Python/Jython script: Note (January 15, 2018): The find function is currently available only in the development version of Open Refine.

For example, split('fire, water, earth, air', ',') returns the array of 4 strings: 'fire', ' water', ' earth' , and ' air'.

Returns an array of strings obtained by splitting s into groups of consecutive characters where the characters within each group share the same unicode type, and consecutive groups differ in their unicode types.

splitByCharType('BE1A3E') = ['BE','1','A','3','E'] Returns an array of strings [ a, frag, b ] where a is the substring within s before the first occurrence of frag in s, and b is the substring after frag in s.

If s does not contain frag, it returns an array of [ s, '', '' ] (the first string is the original s and the second and third strings are empty).

Another Example using Regex with partition(): value.partition(/c.e/)[1] used on a cell with a value of 'abcdefgh' will output 'cde' 'abcdefgh'.partition(/d..g/)[0] will output 'abc', since 'abc' is the 1st part [0], not the middle part [1] which is 'defg', or the tail or last part [-1] which is 'h'.

More info on Regex Returns an array of strings [ a, frag, b ] where a is the substring within s before the last occurrence of frag in s, and b is the substring after frag in s.

If s does not contain frag, it returns an array of [ s, '', '' ] (the first string is the original s and the second and third strings are empty).

Excel FIND and SEARCH functions with formula examples

The tutorial explains the syntax of the Excel FIND and SEARCH functions and provides formula examples of advanced non-trivial uses.

To correctly use a FIND formula in Excel, keep in mind the following simple facts: The SEARCH function in Excel is very similar to FIND in that it also returns the location of a substring in a text string.

And here's a couple of basic Excel SEARCH formulas: =SEARCH('market', 'supermarket') returns 6 because the substring 'market' begins at the 6th character of the word 'supermarket'.

As you see in the screenshot above, the formula SEARCH('function*2013', A2) returns the position of the first character ('f') in the substring if the text string referred to in the within_text argument contains both 'function' and '2013', no matter how many other characters there are in between.

To get the first name, you can use FIND (or SEARCH) in conjunction with the LEFT function: =LEFT(A2, FIND(' ', A2)-1) or =LEFT(A2, SEARCH(' ', A2)-1) As you probably know, the Excel LEFT function returns the specified number of left-most characters in a string.

The LEN function is needed to get the total number of characters in the string, from which you subtract the position of the space: =RIGHT(A2,LEN(A2)-FIND(' ',A2)) or =RIGHT(A2,LEN(A2)-SEARCH(' ',A2)) The following screenshot demonstrates the result:

To return the position of the 3rd occurrence, you embed the above formula in the start_num argument of another FIND function and add 2 to the returned value: =FIND('-',A2, FIND('-', A2, FIND('-',A2)+1) +2)

Another and probably a simpler way of finding the Nth occurrence of a given character is using the Excel FIND function in combination with CHAR and SUBSTITUTE: =FIND(CHAR(1),SUBSTITUTE(A2,'-',CHAR(1),3)) Where '-' is the character in question and '3' is the Nth occurrence you want to find.

So, the general formula is as follows: At first sight, it may seem that the above formulas have little practical value, but the next example will show how useful they are in solving real tasks.

You could use the MID function to return 3 characters from a string, starting at position 4 (skipping the first 2 characters and a dash): =MID(A2, 4, 3) Translated into English, the formula says: 'Look in cell A2, begin extracting from character 4, and return 3 characters'.

The FIND formula to return the position of the 1st dash is as follows: =FIND('-',A2) Because you want to start with the character that follows the dash, add 1 to the returned value and embed the above function in the second argument (start_num) of the MID function: =MID(A2, FIND('-',A2)+1, 3) In this scenario, the Excel SEARCH function works equally well: =MID(A2, SEARCH('-',A2)+1, 3)

For better understanding of this MID formula, let's examine its arguments one by one: In a similar fashion, you can return 3 characters after the 2nd dash: =MID(A2, FIND('-',A2, FIND('-', A2, FIND('-',A2)+1) +2), 3) Or, extract all the characters between the 2nd and 3rd dashes: =MID(A2, FIND('-', A2, FIND('-',A2)+1)+1, FIND('-',A2, FIND('-', A2, FIND('-',A2)+1) +2) - FIND('-', A2, FIND('-',A2)+1)-1)

Find Text in String With Excel SEARCH Function

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

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.

Algorithms in Python: Recursion -- Find Uppercase Letter in String

In this video, we want to solve the following problem: Given a string, develop an algorithm to return the first occurring uppercase letter. We require both an ...

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 ...

Return maximum occurring character in an input string | GeeksforGeeks

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

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 ...

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 ...

Python Find String Method

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

Extract Text from cells in Excel - How to get any word from a cell in Excel

More help: Excel Forum: This tutorial shows you how to input a formula into a .

Excel 2016 - Extract text string using LEFT, MID, RIGHT and FIND

In this video I show you how to extract specific text strings from within a cell using the Excel functions Left, Mid, Right, and Find.