[Algorithm] Coursera Princeton algs4 hello world homework answer (Coursera 普林斯頓算法課 Hello world 作業解答)
![[Algorithm] Coursera Princeton algs4 hello world homework answer (Coursera 普林斯頓算法課 Hello world 作業解答)](/content/images/size/w2000/2023/01/algorithm-cover.png)
簡介
本篇文章為 Coursera 上普林斯頓大學算法課作業的紀錄
環境 (environments)
Computer: Macbook pro 14 M1 pro
IDE: IntelliJ IDEA 2022.3.1 (Ultimate Edition)
OS: MacOs Monterey 12.3
Language: Java
JDK: oracle openjdk-19 (v19.0.2)
環境設置
這個作業總共有三個題目,分別是:
- HelloWorld.java
- HelloGoodbye.java
- RandomWord.java
其中,又以 RandomWord.java 最為特別,因為他要使用 .jar 包作為環境導入,而在 Intellij IDEA 中,又有不同的設置方式.
主要是先把 algs4.jar 包下載下來電腦,在到 Intellij IDEA 中,操作:
File > Project Structure > Dependencies > Add > 1 Jar Or Directories >> Select a .jar file > enable .jar checkbox (開啟剛剛選中的 .jar 包)
以上設置完成了以後,就可以在檔案中 import 課程上面所提供的包並且在 Intellij IDEA 中執行了.
作業內容
- HelloWorld.java
就只是簡單的 Hello World
- HelloGoodbye.java
Command-line arguments. Write a program HelloGoodbye.java that takes two names as command-line arguments and prints hello and goodbye messages as shown below (with the names for the hello message in the same order as the command-line arguments and with the names for the goodbye message in reverse order).
這一題主要是要讓你會接收 Java 的 args 參數,在 Intellij IDEA 中也可以設定執行,在 Program arguments 那一欄輸入你的 command line 參數就可以了
- RandomWord.java
Using algs4.jar. Under construction. Write a program RandomWord.java that reads a sequence of words from standard input and prints one of those words uniformly at random. Do not store the words in an array or list. Instead, use Knuth’s method: when reading the ith word, select it with probability 1/i
to be the champion, replacing the previous champion. After reading all of the words, print the surviving champion.
使用 algs4.jar 撰寫出一個 RandomWord.java 的程式,輸入會是一段文字,使用空白做分隔,要求得輸出是在這一些輸入的字串中,隨機打印出一個文字,注意,不能使用陣列 (array) 儲存這一些字串,隨機選擇的機率公式為 1/i,其中, i 為字串的索引值,使用一個迴圈來處理這串輸入,最後的輸出將是迴圈中最後一個隨機選出的字串,你將會使用到 algs4.jar 包中的一些函數來完成這項作業.
~/Desktop/hello> javac-algs4 RandomWord.java
~/Desktop/hello> java-algs4 RandomWord
heads tails
tails
~/Desktop/hello> java-algs4 RandomWord
heads tails
heads
~/Desktop/hello> more animals8.txt
ant bear cat dog
emu fox goat horse
~/Desktop/hello> java-algs4 RandomWord < animals8.txt
emu
~/Desktop/hello> java-algs4 RandomWord < animals8.txt
bear
Use the following library functions from algs4.jar:
StdIn.readString(): reads and returns the next string from standard input.
讀出字串,他會自動分隔空白,所以讀取n次就會是讀取輸入字串中的第n個.
StdIn.isEmpty(): returns true if there are no more strings available on standard input, and false otherwise.
確認還有沒有可以讀取的輸入,還有回傳 true,反之則是 false.
StdOut.println(): prints a string and terminating newline to standard output. It’s also fine to use System.out.println() instead.
打印字串,基本上和 System.out.println() 函數相同.
StdRandom.bernoulli(p): *returns true with probability p and false with probability 1−p. *
計算機率,可接收多種型態的輸入,int double 等等,返回布林值 bool,來確認是否選中.
In order to access these library functions, you must do the following two things:
Add algs4.jar to the Java classpath. This typically requires a different mechanism from the command line and the IDE.
If you used our autoinstaller, the Bash commands javac-algs4 and java-algs4 add algs4.jar to the Java classpath.
If you use IntelliJ, the supplied IntelliJ project folder includes algs4.jar and adds it to the Java classpath.
If you prefer to use some other shell (such as Powershell or zsh) or IDE (such as Eclipse or Netbeans), that’s fine—just be sure that you can configure it accordingly.
Add an import statement like the following at the top of your program:
import edu.princeton.cs.algs4.StdIn;
import edu.princeton.cs.algs4.StdOut;
import edu.princeton.cs.algs4.StdRandom;
上面這一段是在講怎麼導入課程的 .jar 包