• Complain

it-ebooks - Programming Practice Tutorials (KAIST CS109)

Here you can read online it-ebooks - Programming Practice Tutorials (KAIST CS109) full text of the book (entire story) in english for free. Download pdf and epub, get meaning, cover and reviews about this ebook. year: 2017, publisher: iBooker it-ebooks, genre: Computer. Description of the work, (preface) as well as reviews are available. Best literature library LitArk.com created for fans of good reading and offers a wide selection of genres:

Romance novel Science fiction Adventure Detective Science History Home and family Prose Art Politics Computer Non-fiction Religion Business Children Humor

Choose a favorite category and find really read worthwhile books. Enjoy immersion in the world of imagination, feel the emotions of the characters or learn something new for yourself, make an fascinating discovery.

No cover
  • Book:
    Programming Practice Tutorials (KAIST CS109)
  • Author:
  • Publisher:
    iBooker it-ebooks
  • Genre:
  • Year:
    2017
  • Rating:
    4 / 5
  • Favourites:
    Add to favourites
  • Your mark:
    • 80
    • 1
    • 2
    • 3
    • 4
    • 5

Programming Practice Tutorials (KAIST CS109): summary, description and annotation

We offer to read an annotation, description, summary or preface (depends on what the author of the book "Programming Practice Tutorials (KAIST CS109)" wrote himself). If you haven't found the necessary information about the book — write in the comments, we will try to find it.

it-ebooks: author's other books


Who wrote Programming Practice Tutorials (KAIST CS109)? Find out the surname, the name of the author of the book and a list of all author's works by series.

Programming Practice Tutorials (KAIST CS109) — read online for free the complete book (whole text) full work

Below is the text of the book, divided by pages. System saving the place of the last page read, allows you to conveniently read the book "Programming Practice Tutorials (KAIST CS109)" online for free, without having to search again every time where you left off. Put a bookmark, and you can go to the page where you finished reading at any time.

Light

Font size:

Reset

Interval:

Bookmark:

Make
Programming Practice Tutorials (KAIST CS109)

From: Programming Practice Tutorials

The command line

When I started programming, there were no graphical displays. All computer input and output was done using text.

A computer was shared by many users. Each user was connected to thecomputer from a terminal (which is called this way because it isthe endpoint of the connection from the computer to the user).

The earliest terminals looked like a combination of a printer with akeyboard. (When I was in middle school, I was sometimes allowed toplay with a computer that used such a printing terminal.)

The printers were later replaced by CRT displays that could typicallydisplay a - photo 1

The printers were later replaced by CRT displays that could typicallydisplay a matrix of 25x80 characters (the ASCII alphabet with letters,digits, and some special graphical characters).

Users interacted with the computer by typing a command on thekeyboard The - photo 2

Users interacted with the computer by typing a command on thekeyboard. The computer responded by "printing" the output of thecommand on the terminal.

That's where the name of the Kotlin function println ("printline") (or print in Python, or printf in C) comes froma longtime ago output was really printed on paper.

The first home computers, like the Commodore C-64, were stilltext-based. When the IBM PC came out in the early 1980s, it used atext-based operating system (MS-DOS) until Windows 3.1 appeared, about10 years later.

In graphical user interfaces, which became wide-spread about 25 yearsago, users interact with the computer by clicking with the mouse.That looks easier than writing textual commands, but it is also muchmore limited. You can express commands in text that perform quitecomplicated operations that would take you hundreds of mouse clicks todo by hand. And therefore the command line is still commonly used insoftware development, and some other places: the standard interfaceused by travel agents, for instance, is a command line interface.

In this course we will run and debug Kotlin programs from the commandline (at least at the beginning of the course).

To start the command line in Windows, press Windows+R (that is, holdthe Windows-key and press on "R"). You should see a text fieldwhere you can enter a command. Type cmd to start the Windows command shell .

(If you are using Mac OSX or Linux, you can simply open the"terminal" program. Many of the commands listed below aredifferent, though. For instance, instead of dir you should say ls .)

As always in this tutorial, don't just read this. Try it now .

Here is a list of the most important commands:

  • dir to display the files in the current directory,
  • date /t to display the current date,
  • time /t to display the current time,
  • echo message to display a message,
  • cls to clear the screen,
  • cd to display or to change the current directory,
  • mkdir to make a new directory,
  • type to display the contents of a file,
  • del to delete a file,
  • rename to change the name of a file,
  • help to print a list of the most common commands.
You can usually get help on a certain command by typing the name ofthe command followed by /? , for instance like this:C:\Users\otfried\Documents>rename /?Renames a file or files.RENAME [drive:][path]filename1 filename2.REN [drive:][path]filename1 filename2.Note that you cannot specify a new drive or path for your destination file.C:\Users\otfried\Documents>

Please try this now, and familiarize yourself with interacting withyour computer through the command line. You can use the Tab-key tocomplete a filename, after typing the first one or two characters ofthe filename. You can also use the Up and Down keys to repeatprevious commands.

Introduction to Kotlin

In this section we quickly go through the basic features of Kotlin. Ifyou have programmed before, whether in Python, C, or Java, then thisshould be enough to get you started and to let you write your firstKotlin scripts. (If you have never programmed before, then you need tostudy basic programming first and come back later.)

Incremental testing

When you write a program, it is tempting to write the whole program,and then start debugging it.

In general, this is very bad strategy. It is much more effective to test every function immediately after you have written it.

Continue working on the next function only after the first one workscorrectly.

In Kotlin, we can test functions interactively using the interactivemode. We use the :load command inside the interactive mode. The samecommand allows us to reload our code after we have made changes toit:

$ ktcWelcome to Kotlin version 1.0.1-2 (JRE 1.7.0_95-b00)Type :help for help, :quit for quit>>> :load triangle.kts>>> triangle(3)******>>> triangle(5)***************
A worked out example: the Collatz problem

Let's practice incremental testing on an example, the so-calledCollatz problem.

Consider a sequence of integers following the rule:

\[n_{i+1} = \left\{ \begin{array}{ll}3n_i + 1 & \text{if $n_i$ is odd}\\n_i/2 & \text{if $n_i$ is even}\end{array}\right.\]

If you provide a starting value \(n_0\) , this determines an entiresequence. Here are a few examples, with starting values 5, 34, 7, and 672:

5 16 8 4 2 134 17 52 26 13 40 20 10 5 16 8 4 2 17 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1672 336 168 84 42 21 64 32 16 8 4 2 1You can notice that all four example sequences reach the number 1 (andthen of course the sequence starts cycling: 1 4 2 1 4 2 1 4 2 1...).

It is conjectured that this is always true: for any starting value,the sequence arrives at 1.

We want to do some experiments with this conjecture, for instance todetermine experimentally which starting value gives long chains.So we want to write functions that, given the starting value, canprint out the entire sequence, and can print the number of stepsuntil is reached.

Let's start with the basic function: Given \(n_i\) , compute thenext number \(n_{i+1}\) . I create a filecollatz.kts with thefollowing contents:

fun next(n: Int): Int = if (n % 2 == 0) n / 2 else 3 * n + 1

We test this function by loading the file and checking that itcorrectly handles both the even and the odd case:

$ ktcWelcome to Kotlin version 1.0.1-2 (JRE 1.7.0_95-b00)Type :help for help, :quit for quit>>> :load collatz.kts>>> next(1)4>>> next(4)2>>> next(2)1>>> next(5)16>>> next(16)8>>> next(52)26>>> next(123417432)61708716>>> next(123417431)370252294

The functions seems to work, so the next step is to write a functionthat prints the entire sequence starting from a given starting value.We add the function collatz to our file:

fun collatz(n0: Int) { var n = n0 while (n != 1) { print(n) print(" ") n = next(n) }}I reload the file and test it on an example:>>> :load collatz.kts>>> collatz(5)5 16 8 4 2And already I notice my mistake: the loop doesn't print thefinal . So I change my function as follows(collatz.kts):fun collatz(n0: Int) { var n = n0 while (n != 1) { print(n) print(" ") n = next(n) } println(1)}And I can continue testing by reloading the file and trying again:>>> :load collatz.kts>>> collatz(5)5 16 8 4 2 1>>> collatz(16)16 8 4 2 1>>> collatz(17)17 52 26 13 40 20 10 5 16 8 4 2 1>>> collatz(27)27 82 41 124 62 31 94 47 142 71 214 107 322 161 484 242 121 364 182 91274 137 412 206 103 310 155 466 233 700 350 175 526 263 790 395 1186593 1780 890 445 1336 668 334 167 502 251 754 377 1132 566 283 850 4251276 638 319 958 479 1438 719 2158 1079 3238 1619 4858 2429 7288 36441822 911 2734 1367 4102 2051 6154 3077 9232 4616 2308 1154 577 1732866 433 1300 650 325 976 488 244 122 61 184 92 46 23 70 35 106 53 16080 40 20 10 5 16 8 4 2 1Now it seems to work well.
Next page
Light

Font size:

Reset

Interval:

Bookmark:

Make

Similar books «Programming Practice Tutorials (KAIST CS109)»

Look at similar books to Programming Practice Tutorials (KAIST CS109). We have selected literature similar in name and meaning in the hope of providing readers with more options to find new, interesting, not yet read works.


Reviews about «Programming Practice Tutorials (KAIST CS109)»

Discussion, reviews of the book Programming Practice Tutorials (KAIST CS109) and just readers' own opinions. Leave your comments, write what you think about the work, its meaning or the main characters. Specify what exactly you liked and what you didn't like, and why you think so.