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 reading

for information on printing strings.

(string?'apple)#f procedure(make-stringk[char])→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])→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->list'Apple')'(#\A #\p #\p #\l #\e) procedure(list->stringlst)→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<?'a''b''c')#t procedure(string<=?str1str2...+)→boolean?str1:string?str2:string?Like string<?, but checks whether the arguments are nondecreasing.

(string<=?'a''b''b')#t procedure(string>?str1str2...+)→boolean?str1:string?str2:string?Like string<?, but checks whether the arguments are decreasing.

(string>?'c''b''a')#t procedure(string>=?str1str2...+)→boolean?str1:string?str2:string?Like string<?, but checks whether the arguments are nonincreasing.

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

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

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

(string-ci>?'c''b''a')#t procedure(string-ci>=?str1str2...+)→boolean?str1:string?str2:string?Like string-ci<?, 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<?str1str2...+)→boolean?str1:string?str2:string?Like string<?, 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])→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'>

procedure(string-normalize-spacesstr[sepspace#:trim?trim?#:repeat?repeat?])→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?])→(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?])→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'>

procedure(string-contains?scontained)→boolean?s:string?contained:string?procedure(string-prefix?sprefix)→boolean?s:string?prefix:string?procedure(string-suffix?ssuffix)→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

the start of left-pad-string and right padding ends with the

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

characters long: procedure(~vv...[#:separatorseparator#:widthwidth#:max-widthmax-width#:min-widthmin-width#:limit-markerlimit-marker#:limit-prefix?limit-prefix?#:alignalign#:pad-stringpad-string#:left-pad-stringleft-pad-string#:right-pad-stringright-pad-string])→string?v:any/cseparator:string?=' 'width:(or/cexact-nonnegative-integer?#f)=#fmax-width:(or/cexact-nonnegative-integer?+inf.0)=(orwidth+inf.0)min-width:exact-nonnegative-integer?=(orwidth0)limit-marker:string?='...'limit-prefix?:boolean?=#falign:(or/c'left'center'right)='leftpad-string:non-empty-string?=' 'left-pad-string:non-empty-string?=pad-stringright-pad-string:non-empty-string?=pad-stringLike ~a, but each value is converted like (format '~v' v), the default separator is ' ', and the default limit marker

(~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).'

procedure(~sv...[#:separatorseparator#:widthwidth#:max-widthmax-width#:min-widthmin-width#:limit-markerlimit-marker#:limit-prefix?limit-prefix?#:alignalign#:pad-stringpad-string#:left-pad-stringleft-pad-string#:right-pad-stringright-pad-string])→string?v:any/cseparator:string?=' 'width:(or/cexact-nonnegative-integer?#f)=#fmax-width:(or/cexact-nonnegative-integer?+inf.0)=(orwidth+inf.0)min-width:exact-nonnegative-integer?=(orwidth0)limit-marker:string?='...'limit-prefix?:boolean?=#falign:(or/c'left'center'right)='leftpad-string:non-empty-string?=' 'left-pad-string:non-empty-string?=pad-stringright-pad-string:non-empty-string?=pad-stringLike ~a, but each value is converted like (format '~s' v), the default separator is ' ', and the default limit marker

(~s(list'red''green#'blue'))'(\'red\' green #\'blue\')' procedure(~ev...[#:separatorseparator#:widthwidth#:max-widthmax-width#:min-widthmin-width#:limit-markerlimit-marker#:limit-prefix?limit-prefix?#:alignalign#:pad-stringpad-string#:left-pad-stringleft-pad-string#:right-pad-stringright-pad-string])→string?v:any/cseparator:string?=' 'width:(or/cexact-nonnegative-integer?#f)=#fmax-width:(or/cexact-nonnegative-integer?+inf.0)=(orwidth+inf.0)min-width:exact-nonnegative-integer?=(orwidth0)limit-marker:string?='...'limit-prefix?:boolean?=#falign:(or/c'left'center'right)='leftpad-string:non-empty-string?=' 'left-pad-string:non-empty-string?=pad-stringright-pad-string:non-empty-string?=pad-stringLike ~a, but each value is converted like (format '~e' v), the default separator is ' ', and the default limit marker

(~e(list'red''green#'blue'))''(\'red\' green #\'blue\')' procedure(~rx[#:signsign#:basebase#:precisionprecision#:notationnotation#:format-exponentformat-exponent#:min-widthmin-width#:pad-stringpad-string])→string?x:rational?sign:(or/c#f'+'++'parens(let([ind(or/cstring?(list/cstring?string?))])(list/cindindind)))=#fbase:(or/c(integer-in236)(list/c'up(integer-in236)))=10precision:(or/cexact-nonnegative-integer?(list/c'=exact-nonnegative-integer?))=6notation:(or/c'positional'exponential(->rational?(or/c'positional'exponential)))='positionalformat-exponent:(or/c#fstring?(->exact-integer?string?))=#fmin-width:exact-positive-integer?=1pad-string:non-empty-string?=' 'Converts the rational number x to a string in either positional

The optional arguments control number formatting: procedure(~.av...[#:separatorseparator#:widthwidth#:max-widthmax-width#:min-widthmin-width#:limit-markerlimit-marker#:limit-prefix?limit-prefix?#:alignalign#:pad-stringpad-string#:left-pad-stringleft-pad-string#:right-pad-stringright-pad-string])→string?v:any/cseparator:string?=''width:(or/cexact-nonnegative-integer?#f)=#fmax-width:(or/cexact-nonnegative-integer?+inf.0)=(orwidth+inf.0)min-width:exact-nonnegative-integer?=(orwidth0)limit-marker:string?=''limit-prefix?:boolean?=#falign:(or/c'left'center'right)='leftpad-string:non-empty-string?=' 'left-pad-string:non-empty-string?=pad-stringright-pad-string:non-empty-string?=pad-stringprocedure(~.vv...[#:separatorseparator#:widthwidth#:max-widthmax-width#:min-widthmin-width#:limit-markerlimit-marker#:limit-prefix?limit-prefix?#:alignalign#:pad-stringpad-string#:left-pad-stringleft-pad-string#:right-pad-stringright-pad-string])→string?v:any/cseparator:string?=' 'width:(or/cexact-nonnegative-integer?#f)=#fmax-width:(or/cexact-nonnegative-integer?+inf.0)=(orwidth+inf.0)min-width:exact-nonnegative-integer?=(orwidth0)limit-marker:string?='...'limit-prefix?:boolean?=#falign:(or/c'left'center'right)='leftpad-string:non-empty-string?=' 'left-pad-string:non-empty-string?=pad-stringright-pad-string:non-empty-string?=pad-stringprocedure(~.sv...[#:separatorseparator#:widthwidth#:max-widthmax-width#:min-widthmin-width#:limit-markerlimit-marker#:limit-prefix?limit-prefix?#:alignalign#:pad-stringpad-string#:left-pad-stringleft-pad-string#:right-pad-stringright-pad-string])→string?v:any/cseparator:string?=' 'width:(or/cexact-nonnegative-integer?#f)=#fmax-width:(or/cexact-nonnegative-integer?+inf.0)=(orwidth+inf.0)min-width:exact-nonnegative-integer?=(orwidth0)limit-marker:string?='...'limit-prefix?:boolean?=#falign:(or/c'left'center'right)='leftpad-string:non-empty-string?=' 'left-pad-string:non-empty-string?=pad-stringright-pad-string:non-empty-string?=pad-stringLike ~a, ~v, and ~s, but each v is formatted

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