itu.algs4.stdlib package¶
Submodules¶
itu.algs4.stdlib.binary_out module¶
-
class
itu.algs4.stdlib.binary_out.
BinaryOut
(os=<_io.TextIOWrapper name='<stdout>' mode='w' encoding='UTF-8'>)¶ Bases:
object
-
close
()¶
-
flush
()¶
-
write_bool
(x)¶
-
write_byte
(x)¶
-
write_char
(x)¶
-
write_int
(x)¶
-
write_string
(s)¶
-
-
itu.algs4.stdlib.binary_out.
main
()¶
itu.algs4.stdlib.binary_stdin module¶
-
class
itu.algs4.stdlib.binary_stdin.
BinaryStdIn
¶ Bases:
object
-
EOF
= -1¶
-
buffer_
= 0¶
-
static
close
()¶ Close this input stream and release any associated system resources.
-
ins
= <_io.BufferedReader name=0>¶
-
static
is_empty
()¶
-
is_init
= False¶
-
n
= 0¶
-
static
read_bool
()¶
-
static
read_char
()¶
-
static
read_int
(r=32)¶
-
static
read_string
()¶
-
-
itu.algs4.stdlib.binary_stdin.
main
()¶
itu.algs4.stdlib.binary_stdout module¶
-
class
itu.algs4.stdlib.binary_stdout.
BinaryStdOut
¶ Bases:
object
-
buffer_
= 0¶
-
static
close
()¶
-
static
flush
()¶
-
is_init
= False¶
-
n
= 0¶
-
out
= <_io.BufferedWriter name='<stdout>'>¶
-
static
write_bool
(x)¶
-
static
write_byte
(x)¶
-
static
write_char
(x, r=8)¶
-
static
write_int
(x, r=32)¶
-
write_string
(r=8)¶
-
-
itu.algs4.stdlib.binary_stdout.
main
()¶
itu.algs4.stdlib.color module¶
color.py.
The color module defines the Color class and some popular Color objects.
itu.algs4.stdlib.instream module¶
instream.py.
The instream module defines the InStream class.
-
class
itu.algs4.stdlib.instream.
InStream
(fileOrUrl=None)¶ Bases:
object
An InStream object wraps around a text file or sys.stdin, and supports reading from that stream.
Note: Usually it’s a bad idea to mix these three sets of methods:
– isEmpty(), readInt(), readFloat(), readBool(), readString()
– hasNextLine(), readLine()
- – readAll(), readAllInts(), readAllFloats(), readAllBools(),
- readAllStrings(), readAllLines()
Usually it’s better to use one set exclusively.
-
hasNextLine
()¶ Return True iff the stream wrapped by self has a next line.
-
isEmpty
()¶ Return True iff no non-whitespace characters remain in the stream wrapped by self.
-
readAll
()¶ Read and return as a string all remaining lines of the stream wrapped by self.
-
readAllBools
()¶ Read all remaining strings from the stream wrapped by self, convert each to a bool, and return those bools in an array.
Raise a ValueError if any of the strings cannot be converted to a bool.
-
readAllFloats
()¶ Read all remaining strings from the stream wrapped by self, convert each to a float, and return those floats in an array.
Raise a ValueError if any of the strings cannot be converted to a float.
-
readAllInts
()¶ Read all remaining strings from the stream wrapped by self, convert each to an int, and return those ints in an array.
Raise a ValueError if any of the strings cannot be converted to an int.
-
readAllLines
()¶ Read all remaining lines from the stream wrapped by self, and return them as strings in an array.
-
readAllStrings
()¶ Read all remaining strings from the stream wrapped by self, and return them in an array.
-
readBool
()¶ Discard leading white space characters from the stream wrapped by self.
Then read from the stream a sequence of characters comprising a bool. Convert the sequence of characters to an bool, and return the bool. Raise an EOFError if no non-whitespace characters remain in the stream. Raise a ValueError if the next characters to be read from the stream cannot comprise an bool.
-
readFloat
()¶ Discard leading white space characters from the stream wrapped by self.
Then read from the stream a sequence of characters comprising a float. Convert the sequence of characters to an float, and return the float. Raise an EOFError if no non-whitespace characters remain in the stream. Raise a ValueError if the next characters to be read from the stream cannot comprise a float.
-
readInt
()¶ Discard leading white space characters from the stream wrapped by self.
Then read from the stream a sequence of characters comprising an integer. Convert the sequence of characters to an integer, and return the integer. Raise an EOFError if no non-whitespace characters remain in the stream. Raise a ValueError if the next characters to be read from the stream cannot comprise an integer.
-
readLine
()¶ Read and return as a string the next line of the stream wrapped by self.
Raise an EOFError is there is no next line.
-
readString
()¶ Discard leading white space characters from the stream wrapped by self.
Then read from the stream a sequence of characters comprising a string, and return the string. Raise an EOFError if no non- whitespace characters remain in the stream.
itu.algs4.stdlib.outstream module¶
outstream.py.
The outstream module defines the OutStream class.
-
class
itu.algs4.stdlib.outstream.
OutStream
(f=None)¶ Bases:
object
An OutStream object wraps around a text file or sys.stdout, and supports writing to that stream.
-
write
(x='')¶ Write x to the stream wrapped by self.
-
writef
(fmt, *args)¶ Write each element of args to the stream wrapped by self.
Use the format specified by string fmt.
-
writeln
(x='')¶ Write x and an end-of-line mark to the stream wrapped by self.
-
itu.algs4.stdlib.picture module¶
picture.py.
The picture module defines the Picture class.
-
class
itu.algs4.stdlib.picture.
Picture
(arg1=None, arg2=None)¶ Bases:
object
A Picture object models an image.
It is initialized such that it has a given width and height and contains all black pixels. Subsequently you can load an image from a given JPG or PNG file.
-
get
(x, y)¶ Return the color of self at location (x, y).
-
height
()¶ Return the height of self.
-
save
(f)¶ Save self to the file whose name is f.
-
set
(x, y, c)¶ Set the color of self at location (x, y) to c.
-
width
()¶ Return the width of self.
-
itu.algs4.stdlib.stdarray module¶
stdarray.py.
The stdarray module defines functions related to creating, reading, and writing one- and two-dimensional arrays.
-
itu.algs4.stdlib.stdarray.
create1D
(length, value=None)¶ Create and return a 1D array containing length elements, each initialized to value.
-
itu.algs4.stdlib.stdarray.
create2D
(rowCount, colCount, value=None)¶ Create and return a 2D array having rowCount rows and colCount columns, with each element initialized to value.
-
itu.algs4.stdlib.stdarray.
readBool1D
()¶ Read from sys.stdin and return an array of booleans.
An integer at the beginning of sys.stdin defines the array’s length.
-
itu.algs4.stdlib.stdarray.
readBool2D
()¶ Read from sys.stdin and return a two-dimensional array of booleans.
Two integers at the beginning of sys.stdin define the array’s dimensions.
-
itu.algs4.stdlib.stdarray.
readFloat1D
()¶ Read from sys.stdin and return an array of floats.
An integer at the beginning of sys.stdin defines the array’s length.
-
itu.algs4.stdlib.stdarray.
readFloat2D
()¶ Read from sys.stdin and return a two-dimensional array of floats.
Two integers at the beginning of sys.stdin define the array’s dimensions.
-
itu.algs4.stdlib.stdarray.
readInt1D
()¶ Read from sys.stdin and return an array of integers.
An integer at the beginning of sys.stdin defines the array’s length.
-
itu.algs4.stdlib.stdarray.
readInt2D
()¶ Read from sys.stdin and return a two-dimensional array of integers.
Two integers at the beginning of sys.stdin define the array’s dimensions.
-
itu.algs4.stdlib.stdarray.
write1D
(a)¶ Write array a to sys.stdout.
First write its length. bool objects are written as 0 and 1, not False and True.
-
itu.algs4.stdlib.stdarray.
write2D
(a)¶ Write two-dimensional array a to sys.stdout.
First write its dimensions. bool objects are written as 0 and 1, not False and True.
itu.algs4.stdlib.stdaudio module¶
itu.algs4.stdlib.stddraw module¶
stddraw.py.
The stddraw module defines functions that allow the user to create a drawing. A drawing appears on the canvas. The canvas appears in the window. As a convenience, the module also imports the commonly used Color objects defined in the color module.
-
itu.algs4.stdlib.stddraw.
circle
(x, y, r)¶ Draw on the background canvas a circle of radius r centered on (x, y).
-
itu.algs4.stdlib.stddraw.
clear
(c=<itu.algs4.stdlib.color.Color object>)¶ Clear the background canvas to color c, where c is an object of class color.Color.
c defaults to stddraw.WHITE.
-
itu.algs4.stdlib.stddraw.
filledCircle
(x, y, r)¶ Draw on the background canvas a filled circle of radius r centered on (x, y).
-
itu.algs4.stdlib.stddraw.
filledPolygon
(x, y)¶ Draw on the background canvas a filled polygon with coordinates (x[i], y[i]).
-
itu.algs4.stdlib.stddraw.
filledRectangle
(x, y, w, h)¶ Draw on the background canvas a filled rectangle of width w and height h whose lower left point is (x, y).
-
itu.algs4.stdlib.stddraw.
filledSquare
(x, y, r)¶ Draw on the background canvas a filled square whose sides are of length 2r, centered on (x, y).
-
itu.algs4.stdlib.stddraw.
hasNextKeyTyped
()¶ Return True if the queue of keys the user typed is not empty.
Otherwise return False.
-
itu.algs4.stdlib.stddraw.
line
(x0, y0, x1, y1)¶ Draw on the background canvas a line from (x0, y0) to (x1, y1).
-
itu.algs4.stdlib.stddraw.
mousePressed
()¶ Return True if the mouse has been left-clicked since the last time mousePressed was called, and False otherwise.
-
itu.algs4.stdlib.stddraw.
mouseX
()¶ Return the x coordinate in user space of the location at which the mouse was most recently left-clicked.
If a left-click hasn’t happened yet, raise an exception, since mouseX() shouldn’t be called until mousePressed() returns True.
-
itu.algs4.stdlib.stddraw.
mouseY
()¶ Return the y coordinate in user space of the location at which the mouse was most recently left-clicked.
If a left-click hasn’t happened yet, raise an exception, since mouseY() shouldn’t be called until mousePressed() returns True.
-
itu.algs4.stdlib.stddraw.
nextKeyTyped
()¶ Remove the first key from the queue of keys that the the user typed, and return that key.
-
itu.algs4.stdlib.stddraw.
picture
(pic, x=None, y=None)¶ Draw pic on the background canvas centered at (x, y).
pic is an object of class picture.Picture. x and y default to the midpoint of the background canvas.
-
itu.algs4.stdlib.stddraw.
point
(x, y)¶ Draw on the background canvas a point at (x, y).
-
itu.algs4.stdlib.stddraw.
polygon
(x, y)¶ Draw on the background canvas a polygon with coordinates (x[i], y[i]).
-
itu.algs4.stdlib.stddraw.
rectangle
(x, y, w, h)¶ Draw on the background canvas a rectangle of width w and height h whose lower left point is (x, y).
-
itu.algs4.stdlib.stddraw.
save
(f)¶ Save the window canvas to file f.
-
itu.algs4.stdlib.stddraw.
setCanvasSize
(w=512, h=512)¶ Set the size of the canvas to w pixels wide and h pixels high.
Calling this function is optional. If you call it, you must do so before calling any drawing function.
-
itu.algs4.stdlib.stddraw.
setFontFamily
(f='Helvetica')¶ Set the font family to f (e.g. ‘Helvetica’ or ‘Courier’).
-
itu.algs4.stdlib.stddraw.
setFontSize
(s=12)¶ Set the font size to s (e.g. 12 or 16).
-
itu.algs4.stdlib.stddraw.
setPenColor
(c=<itu.algs4.stdlib.color.Color object>)¶ Set the pen color to c, where c is an object of class color.Color.
c defaults to stddraw.BLACK.
-
itu.algs4.stdlib.stddraw.
setPenRadius
(r=0.005)¶ Set the pen radius to r, thus affecting the subsequent drawing of points and lines.
If r is 0.0, then points will be drawn with the minimum possible radius and lines with the minimum possible width.
-
itu.algs4.stdlib.stddraw.
setXscale
(min=0.0, max=1.0)¶ Set the x-scale of the canvas such that the minimum x value is min and the maximum x value is max.
-
itu.algs4.stdlib.stddraw.
setYscale
(min=0.0, max=1.0)¶ Set the y-scale of the canvas such that the minimum y value is min and the maximum y value is max.
-
itu.algs4.stdlib.stddraw.
show
(msec=inf)¶ Copy the background canvas to the window canvas, and then wait for msec milliseconds.
msec defaults to infinity.
-
itu.algs4.stdlib.stddraw.
square
(x, y, r)¶ Draw on the background canvas a square whose sides are of length 2r, centered on (x, y).
-
itu.algs4.stdlib.stddraw.
text
(x, y, s)¶ Draw string s on the background canvas centered at (x, y).
itu.algs4.stdlib.stdio module¶
stdio.py.
The stdio module supports reading from standard input and writing to sys.stdout.
Note: Usually it’s a bad idea to mix these three sets of reading functions:
– isEmpty(), readInt(), readFloat(), readBool(), readString()
– hasNextLine(), readLine()
- – readAll(), readAllInts(), readAllFloats(), readAllBools(),
- readAllStrings(), readAllLines()
Usually it’s better to use one set exclusively.
-
itu.algs4.stdlib.stdio.
eprint
(*args, **kwargs)¶
-
itu.algs4.stdlib.stdio.
hasNextLine
()¶ Return True if standard input has a next line.
Otherwise return False.
-
itu.algs4.stdlib.stdio.
isEmpty
()¶ Return True if no non-whitespace characters remain in standard input.
Otherwise return False.
-
itu.algs4.stdlib.stdio.
readAll
()¶ Read and return as a string all remaining lines of standard input.
-
itu.algs4.stdlib.stdio.
readAllBools
()¶ Read all remaining strings from standard input, convert each to a bool, and return those bools in an array.
Raise a ValueError if any of the strings cannot be converted to a bool.
-
itu.algs4.stdlib.stdio.
readAllFloats
()¶ Read all remaining strings from standard input, convert each to a float, and return those floats in an array.
Raise a ValueError if any of the strings cannot be converted to a float.
-
itu.algs4.stdlib.stdio.
readAllInts
()¶ Read all remaining strings from standard input, convert each to an int, and return those ints in an array.
Raise a ValueError if any of the strings cannot be converted to an int.
-
itu.algs4.stdlib.stdio.
readAllLines
()¶ Read all remaining lines from standard input, and return them as strings in an array.
-
itu.algs4.stdlib.stdio.
readAllStrings
()¶ Read all remaining strings from standard input, and return them in an array.
-
itu.algs4.stdlib.stdio.
readBool
()¶ Discard leading white space characters from standard input. Then read from standard input a sequence of characters comprising a bool. Convert the sequence of characters to a bool, and return the bool. Raise an EOFError if no non-whitespace characters remain in standard input. Raise a ValueError if the next characters to be read from standard input cannot comprise a bool.
These character sequences can comprise a bool: – True – False – 1 (means true) – 0 (means false)
-
itu.algs4.stdlib.stdio.
readFloat
()¶ Discard leading white space characters from standard input.
Then read from standard input a sequence of characters comprising a float. Convert the sequence of characters to a float, and return the float. Raise an EOFError if no non-whitespace characters remain in standard input. Raise a ValueError if the next characters to be read from standard input cannot comprise a float.
-
itu.algs4.stdlib.stdio.
readInt
()¶ Discard leading white space characters from standard input.
Then read from standard input a sequence of characters comprising an integer. Convert the sequence of characters to an integer, and return the integer. Raise an EOFError if no non-whitespace characters remain in standard input. Raise a ValueError if the next characters to be read from standard input cannot comprise an integer.
-
itu.algs4.stdlib.stdio.
readLine
()¶ Read and return as a string the next line of standard input.
Raise an EOFError is there is no next line.
-
itu.algs4.stdlib.stdio.
readString
()¶ Discard leading white space characters from standard input.
Then read from standard input a sequence of characters comprising a string, and return the string. Raise an EOFError if no non- whitespace characters remain in standard input.
-
itu.algs4.stdlib.stdio.
write
(x='')¶ Write x to standard output.
-
itu.algs4.stdlib.stdio.
writef
(fmt, *args)¶ Write each element of args to standard output.
Use the format specified by string fmt.
-
itu.algs4.stdlib.stdio.
writeln
(x='')¶ Write x and an end-of-line mark to standard output.
itu.algs4.stdlib.stdrandom module¶
stdrandom.py.
The stdrandom module defines functions related to pseudo-random numbers.
-
itu.algs4.stdlib.stdrandom.
bernoulli
(p=0.5)¶ Return True with probability p.
-
itu.algs4.stdlib.stdrandom.
binomial
(n, p=0.5)¶ Return the number of heads in n coin flips, each of which is heads with probability p.
-
itu.algs4.stdlib.stdrandom.
discrete
(a)¶ Return a float from a discrete distribution: i with probability a[i].
Precondition: the elements of array a sum to 1.
-
itu.algs4.stdlib.stdrandom.
exp
(lambd)¶ Return a float from an exponential distribution with rate lambd.
-
itu.algs4.stdlib.stdrandom.
gaussian
(mean=0.0, stddev=1.0)¶ Return a float according to a standard Gaussian distribution with the given mean (mean) and standard deviation (stddev).
-
itu.algs4.stdlib.stdrandom.
seed
(i=None)¶ Seed the random number generator as hash(i), where i is an int.
If i is None, then seed using the current time or, quoting the help page for random.seed(), “an operating system specific randomness source if available.”
-
itu.algs4.stdlib.stdrandom.
shuffle
(a)¶ Shuffle array a.
-
itu.algs4.stdlib.stdrandom.
uniform
(hi)¶ Return an integer chosen uniformly from the range [0, hi).
-
itu.algs4.stdlib.stdrandom.
uniformFloat
(lo, hi)¶ Return a number chosen uniformly from the range [lo, hi).
-
itu.algs4.stdlib.stdrandom.
uniformInt
(lo, hi)¶ Return an integer chosen uniformly from the range [lo, hi).
itu.algs4.stdlib.stdstats module¶
stdstats.py.
The stdstats module defines functions related to statistical analysis and graphical data display.
-
itu.algs4.stdlib.stdstats.
mean
(a)¶ Return the average of the elements of array a.
-
itu.algs4.stdlib.stdstats.
median
(a)¶ Return the median of the elements of array a.
-
itu.algs4.stdlib.stdstats.
plotBars
(a)¶ Plot the elements of array a as bars.
-
itu.algs4.stdlib.stdstats.
plotLines
(a)¶ Plot the elements of array a as line end-points.
-
itu.algs4.stdlib.stdstats.
plotPoints
(a)¶ Plot the elements of array a as points.
-
itu.algs4.stdlib.stdstats.
stddev
(a)¶ Return the standard deviation of the elements of array a.
-
itu.algs4.stdlib.stdstats.
var
(a)¶ Return the sample variance of the elements of array a.
Module contents¶
This module is based on the code at https://introcs.cs.princeton.edu/python/code/ written by Robert Sedgewick, Kevin Wayne, and Robert Dondero.