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

for information on printing strings.

(string?'apple)#f procedure(make-stringk[char])&rarr;string?k:exact-nonnegative-integer?char:char?=#\nul Returns a new mutable string of length k where each

string whose length is the number of provided chars, and whose

str, returning str itself if str is

Returns the character at position k in str.

The first position in the string corresponds to 0, so the

character position k in str to char.

position in the string corresponds to 0, so the position

k must be less than the length of the string, otherwise the

s'Apply' procedure(substringstrstart[end])&rarr;string?str:string?start:exact-nonnegative-integer?end:exact-nonnegative-integer?=(string-lengthstr)Returns a new mutable string that is (- end start)

that case the destination region can overlap with the source region;

the destination characters after the copy match the source characters

(string-&gt;list'Apple')'(#\A #\p #\p #\l #\e) procedure(list-&gt;stringlst)&rarr;string?lst:(listofchar?) Returns a new

mutable string whose content is the list of characters in lst.

.char?)Creates a string of n characters by applying proc to the

is the resulting string, then (string-ref str i) is the character produced by (proc i).

(string&lt;?'a''b''c')#t procedure(string&lt;=?str1str2...+)&rarr;boolean?str1:string?str2:string?Like string&lt;?, but checks whether the arguments are nondecreasing.

(string&lt;=?'a''b''b')#t procedure(string&gt;?str1str2...+)&rarr;boolean?str1:string?str2:string?Like string&lt;?, but checks whether the arguments are decreasing.

(string&gt;?'c''b''a')#t procedure(string&gt;=?str1str2...+)&rarr;boolean?str1:string?str2:string?Like string&lt;?, but checks whether the arguments are nonincreasing.

(string-ci=?'a''a''a')#t procedure(string-ci&lt;?str1str2...+)&rarr;boolean?str1:string?str2:string?Like string&lt;?, but checks whether the arguments would be in

(string-ci&lt;?'a''b''c')#t procedure(string-ci&lt;=?str1str2...+)&rarr;boolean?str1:string?str2:string?Like string-ci&lt;?, but checks whether the arguments would be nondecreasing after case-folding.

(string-ci&lt;=?'a''b''b')#t procedure(string-ci&gt;?str1str2...+)&rarr;boolean?str1:string?str2:string?Like string-ci&lt;?, but checks whether the arguments would be decreasing after case-folding.

(string-ci&gt;?'c''b''a')#t procedure(string-ci&gt;=?str1str2...+)&rarr;boolean?str1:string?str2:string?Like string-ci&lt;?, but checks whether the arguments would be nonincreasing after case-folding.

whose characters are the upcase conversion of the characters in

conversion rules that map code-point sequences to code-point

sequences (instead of simply mapping a 1-to-1 function on code points

given string is already in the corresponding Unicode normal form, the

string may be returned directly as the result (instead of a newly allocated

procedure(string-locale&lt;?str1str2...+)&rarr;boolean?str1:string?str2:string?Like string&lt;?, but the sort order compares strings in a locale-specific

(require racket/string)package: baseThe bindings documented in this section are provided by the racket/string and racket libraries, but not racket/base.

(string-append*(cdr(append*(map(lambda(x)(list', 'x))'('Alpha''Beta''Gamma')))))'Alpha, Beta, Gamma' procedure(string-joinstrs[sep#:before-firstbefore-first#:before-lastbefore-last#:after-lastafter-last])&rarr;string?strs:(listofstring?)sep:string?=' 'before-first:string?=''before-last:string?=sepafter-last:string?=''Appends the strings in strs, inserting sep between each

(string-join'('one''two''three''four')' potato ')'one potato two potato three potato four'&gt;

procedure(string-normalize-spacesstr[sepspace#:trim?trim?#:repeat?repeat?])&rarr;string?str:string?sep:(or/cstring?regexp?)=#px'\\s+'space:string?=' 'trim?:any/c=#trepeat?:any/c=#fNormalizes spaces in the input str by trimming it (using string-trim

(string-normalize-spaces'foo barbaz \r\n\t')'foo bar baz' The result of (string-normalize-spaces str sep space) is the same as

(string-join (string-split str sep ....) space).

(string-replace'foo bar baz''bar''blah')'foo blah baz' procedure(string-splitstr[sep#:trim?trim?#:repeat?repeat?])&rarr;(listofstring?)str:string?sep:(or/cstring?regexp?)=#px'\\s+'trim?:any/c=#trepeat?:any/c=#fSplits the input str on whitespaces, returning a list of substrings

is the empty string after trimming, the result is '()

(string-split''#:trim?#f)'('' '') procedure(string-trimstr[sep#:left?left?#:right?right?#:repeat?repeat?])&rarr;string?str:string?sep:(or/cstring?regexp?)=#px'\\s+'left?:any/c=#tright?:any/c=#trepeat?:any/c=#fTrims the input str by removing prefix and suffix sep, which

(string-trim'foo barbaz \r\n\t'' '#:repeat?#t)'foo barbaz \r\n\t'&gt;

procedure(string-contains?scontained)&rarr;boolean?s:string?contained:string?procedure(string-prefix?sprefix)&rarr;boolean?s:string?prefix:string?procedure(string-suffix?ssuffix)&rarr;boolean?s:string?suffix:string?Checks whether s includes at any location, start with, or ends with the

(require racket/format)package: baseThe bindings documented in this section are provided by the racket/format and racket libraries, but not racket/base.

(~a(expt6.187))'2.1071509386211452e+68' The ~a function is primarily useful for strings, numbers, and other atomic

The ~v and ~s functions are better suited to compound

(~a'orange'#:min-width20#:align'center#:left-pad-string'- '#:right-pad-string' -')'- - - -orange- - - -' Use width to set both max-width and min-width simultaneously,

(~v(list'red''green#'blue'))''(\'red\' green #\'blue\')' Use ~v to produce text that talks about Racket values.

(let([nums(for/list([i10])i)])(~a'The even numbers in '(~vnums)' are '(~v(filtereven?nums))'.'))'The even numbers in '(0 1 2 3 4 5 6 7 8 9) are '(0 2 4 6 8).'

BluePrism - String Reg Expressions || Reality & Useful

Hi Friends, Now we will discuss about 'Utility - String' Regular expressions -------------------------------------------------------------------------- Why we need to use regular ...

Find Special Character in a string using Sql

In this video we learn how to find the special character , number in a string using sql server. -- How to Find Number and character in a string SELECT ...

SPSSisFun: Converting Text (string) data to Numeric data

In this video I show you how to convert text data into numeric data. If you have any questions please feel free to post them in the comments section below and I ...

Convert Text to Numbers or Numbers to Text

Check out my Blog: How to convert or change text to numbers using several methods. Also how to convert or change ..

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

Counting Word Frequency using a Dictionary (Chapter 9)

- Python for Everybody: Exploring Data in Python 3.0 Please visit the web site to access a free textbook, free supporting materials, as well ..

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 .

Character frequency in a String using Java

Finding out how many times each individual character appears in a string using Java language. Note:"I meant to say 'j' at 7:58 ."

Ramanujan's Pi Formula

The second video in a series about Ramanujan. Continuing the biography and a look at another of Ramanujan's formulas. This one involves Ramanujan's pi ...