Recap - Username Builder
Part of the Logic & Flow section of Coddy's R journey. Lesson 6 of 64.
Challenge
MediumComplete make_username(full_name, year) to build a username:
- Take the words of the name. The name may have extra spaces between or around the words.
- Use the first letter of the first word and the whole last word, in lower case.
- Remove every
-and'from the result. - Add the last two digits of
year.
For Ada Lovelace and 1815 the username is alovelace15. The function returns two lines: Username: followed by the username, and Length: followed by its number of characters.
The supplied code reads the name and the year and prints each returned line on its own line.
Try it yourself
make_username <- function(full_name, year) {
# Write your code here
c("Username: ", "Length: 0")
}
# Supplied input/output code: keep it as it is
input <- suppressWarnings(readLines(file("stdin")))
cat(make_username(input[1], input[2]), sep = "\n")
All lessons in Logic & Flow
1Strings In Depth
Substrings with substr()Formatting with sprintf()Splitting and JoiningSearching StringsReplacing TextRecap - Username Builder4Matrices
Creating MatricesIndexing MatricesRow and Column SummariesMatrix ArithmeticRecap - Seating Chart2Key-Value Lookups
Named Vector LookupsChecking KeysAdding and Removing KeysLooping Over NamesRecap - Stock Desk3Sets and Counting
Unique ValuesSet OperationsMembership TestsCounting with table()Recap - Event Guests6Functions as Values
Anonymous FunctionsPassing FunctionsReturning FunctionsClosures with StateRecap - Discount Rules9Data Frames
Creating Data FramesColumns and RowsFiltering RowsAdding and SortingRecap - Sales Report12Project - Expense Tracker
Recording ExpensesTotal SpendingPractice on your own: Online R compiler