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')

Octave supports a wide range of functions for manipulating strings. Since

a string is just a matrix, simple manipulations can be accomplished using

The following example shows how to replace all

For more complex manipulations, such as searching, replacing, and general

regular expressions, the following functions come with Octave.

If s is a matrix, deblank trims each row to the length of longest

If s is a cell array of strings, operate recursively

If s is a matrix, strtrim trims each row to the length of longest

If s is a cell array of strings, operate recursively on

If s is a character matrix, then the number of columns is adjusted.

each cell element and the new cell array is returned.

If the optional argument overlap is true (default), the returned vector

See also: strfind, strmatch, strcmp, strncmp, strcmpi, strncmpi, find.

If a cell array of strings cellstr is specified then idx is a cell

Join the elements of the cell string array, cstr, into a single string.

If delimiter is specified as a string, the cell string array is joined

If delimiter is a cell string array whose length is one less than cstr,

The second argument A must be a string, character matrix, or a cell array

See also: strfind, findstr, strcmp, strncmp, strcmpi, strncmpi, find.

str may also be a cell array of strings in which case the function executes

on every individual string and returns a cell array of tokens and remainders.

If rem is requested, it contains the remainder of the string, starting at

Split the string str using the delimiters specified by del and return

", "\f", "\n", "\r", "\t", "\v"}.

The optional second output, matches, returns the delimiters which were matched

2-D character arrays are split at separators and at the original column boundaries.

The string str is split into words that are repeatedly matched to the specifiers

words than specifiers, the process is repeated until all words have been

The string format describes how the words in str should be parsed.

For %s and %d, %f, %n, %u and the associated %*s …

In addition the format may contain literal character strings;

By default, format is "%f", meaning that numbers are read from str.

Optional numeric argument format_repeat can be used for limiting the number

0 (zero) is an acceptable value for format_repeat.

The behavior of strread can be changed via property-value pairs.

Any character in value will be used to split str into words (default

Value to return for empty numeric values in non-whitespace delimited data. The

When the data type does not support NaN (int32 for example),

Treat single occurrences (surrounded by delimiters or whitespace) of the string(s)

If value true (1, default), ignore read errors and return normally. If

string defining whitespace must be enclosed in double quotes for proper processing

data field, multiple consecutive whitespace characters are collapsed into

If whitespace characters are to be kept as-is (in e.g.,

strings), specify an empty value (i.e., "") for "whitespace";

When the number of words in str doesn’t match an exact multiple of the

number of format conversion specifiers, strread’s behavior depends on the

Data columns are padded with empty fields or NaN so that all columns have equal

str and ptn can be ordinary strings, cell array of strings, or character

value for len extracts to within len characters of the

Search for pat in str and return the positions and substrings of any

The matched pattern pat can include any of the standard regex operators,

The pattern will match any character listed between "["

Escape sequences defined below can also be used inside list operators.

Requires pattern to occur at the start (^) or end

In addition, the following escaped characters have special meaning.

The outputs of regexp default to the order given below

The extents of each matched token surrounded by (…) in pat

structure containing the text of each matched named token, with the name being

cell array of the text not returned by match, i.e., what remains if you split

Particular output arguments, or the order of the output arguments, can be selected

All characters in the pattern, including whitespace, are significant and are used

The pattern may include arbitrary whitespace and also comments beginning with

regexp ('a', 'b*', 'emptymatch') returns [1 2] because there are

zero or more 'b' characters at positions 1 and end-of-string.

that reduce the number of matches (e.g., by creatively using set complement),

and then further processing the return variables (now reduced in size)

Search for pat in str and return the positions and substrings of any

The replacement string may contain $i, which substitutes for the ith set

This may include either wildcard replacement or special character escaping.

The input, t, may be either a 2-D character array, or a cell array of character

The tab width is specified by tw, and defaults to eight.

If the optional argument deblank is true, then the spaces will be removed

The following example reads a file and writes an untabified version of the same

String.prototype.indexOf()

The indexOf() method returns the index within the calling String object of the first occurrence of the specified value, starting the search at fromIndex.

Therefore, when checking if a specific string exists within another string the correct way to check would be: The following example uses indexOf() to locate values in the string 'Brave new world'.

The following example sets count to the number of occurrences of the letter e in the string str: The compatibility table in this page is generated from structured data.

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 .

Extract Text Between Two Characters in Excel

Like this tip? Find more at ☆ Extract text between two characters in a cell. This video looks at how to extract text between two forward ..

Excel Magic Trick 323: Partial Text VLOOKUP (Fuzzy Match)

Two formulas: VLOOKUP function with wild card criteria for the lookup_value and LOOKUP / SEARCH functions formula with 2^15 number. See what happens ...

Python Count String Method

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

Excel Magic Trick 559: Extract Nth Word In Text String

See how to Extract First, Last or Nth Word In Text String using the following functions: SUBSTITUTE, LEN, SEARCH, REPLACE, LEFT, RIGHT, MID.

Contains and IndexOf String Functions - VB .Net

In this tutorial I will explain and demonstrate the IndexOf and Contains functions. Contains is a boolean function and it simply returns true or false based on ...

Knuth–Morris–Pratt(KMP) Pattern Matching(Substring search)

Pattern matching(substring search) using KMP algorithm ..

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.

04 C | How to count word in a string

In this video we will learn to count words using C programming language. The concept is really simple and you can try it out on your favorite programming ...