Exercise 5.1
Use the data set from stringr::sentences for this exercise.
Create a for-loop structure to get the number of characters for all sentences in the stringr::sentences. Present your results in a data frame of three columns:
5.1.1). Column 1 includes an unique integer ID for each sentence; 5.1.2). Column 2 includes the texts of each sentence; 5.1.3). Column 3 includes the number of characters of each sentence.
Note: A for-loop structure is needed for this exercise. But it may not necessarily be the most efficient way.
Exercise 5.2
Use the data set stringr::fruit for this exercise.
Create a for-loop and if-statement to instruct R to go through each fruit name and print out those fruit names that start with the vowel letters only (i.e., a, e, i, o, u).
Exercise 5.3
In the above example, if the guess is smaller than ans, our script works fine. However, if the guess is larger than ans, then our script will crash.
How to fix it?
Exercise 5.4
The above guessMyNumber() can be improved. Sometimes naughty (careless) users would not input numbers as requested. Instead, they may accidentally (or on purpose) enter characters that are NOT digits at all in their guesses.
How can you adjust the guessMyNumber() so that when users enter non-digit characters, your program will send out a warning message automatically?
An example output from the revised version is provided below.
guessMyNumber_v2() Please guess my number(0~100):12 The asnwer is HIGHER. Please guess my number(0~100):80 The asnwer is LOWER Please guess my number(0~100):1000 The asnwer is LOWER Please guess my number(0~100):a Please behave. Enter an INTEGER!!! Please guess my number(0~100):df Please behave. Enter an INTEGER!!! Please guess my number(0~100):
13
Please behave. Enter an INTEGER!!! Please guess my number(0~100):frw Please behave. Enter an INTEGER!!! Please guess my number(0~100):50 The asnwer is LOWER Please guess my number(0~100):30 The asnwer is HIGHER. Please guess my number(0~100):40 The asnwer is HIGHER. Please guess my number(0~100):45 The asnwer is LOWER Please guess my number(0~100):43 The asnwer is LOWER Please guess my number(0~100):42 The asnwer is LOWER Please guess my number(0~100):41 Correct!
Aucun commentaire:
Enregistrer un commentaire