Skip to content
Snippets Groups Projects
Commit 2a96446e authored by AB8057's avatar AB8057
Browse files

Upload New File

parent b5a17dec
No related branches found
No related tags found
No related merge requests found
#Part 1
#Declare a keyword list that contains name of the color and color html value.
#Add at least 10 colors to the keyword list
#Create a loop that asks user the color name or color html value.
#If entered text begins with '#', print the corresponding color name.
#Otherwise print the corresponding html color value.
#If neither is found in keyword list, exit the loop.
#this was a bit harder, as creating a working loop took a lot of time.
#as a result the structure of the code became a bit complex
defmodule ColorMatcher do
@colors [ #keywordlist is declared here
{:azure, "#F0FFFF"},
{:coral, "#FF7F50"},
{:indigo, "#4B0082"},
{:navy, "#000080"},
{:orchid, "#DA70D6"},
{:silver, "#C0C0C0"},
{:teal, "#008080"},
{:thistle, "#D8BFD8"},
{:wheat, "#F5DEB3"},
{:white, "#FFFFFF"}
]
defp find_color(input) do #this function tries to find the color that user gave
input_trim = String.trim(input) #whitespaces etc. get trimmed off the given user input
case String.starts_with?(input_trim, "#") do #the case checks whether input starts with "#" or not
true -> find_by_html_value(input_trim) #if it starts with "#", it calls a function to find the color by value
false -> find_by_color_name(input_trim) #if it does not, it calls a function to find the color by name
end
end
defp find_by_html_value(html_value) do
case Enum.find(@colors, fn {_name, value} -> value == html_value end) do #case to find a matching HMTL value
{name, _} -> #if match is found, color name is printed and true is returned
IO.puts("Color name for #{html_value}: #{name}")
true
nil -> false #if not, false is returned
end
end
defp find_by_color_name(color_name) do
case Enum.find(@colors, fn {name, _value} -> name == String.to_atom(color_name) end) do
{_, value} -> #if match found, color value is printed and true is returned
IO.puts("HTML value for #{color_name}: #{value}")
true
nil -> false #if not, return false
end
end
def start_loop() do #this function initiates color match finder loop
do_while(fn -> loop() end)
end
#loop's operating principles are defined in this function
defp loop() do
IO.puts("Enter a color name or HTML value:")
input = IO.gets("") |> String.trim() #user's input is taken here and trimmed of whitespaces etc.
case input do
_ ->
case find_color(input) do #checking if a match is found for the user's input
true -> :continue
false -> #if there is no match, loop is exited
IO.puts("No matching color found. Exiting.")
:ok
end
end
end
defp do_while(block) do #this function executes block in the loop until :continue is not returned
case block.() do
:continue -> do_while(block)
_ -> :ok
end
end
end
ColorMatcher.start_loop() #calls the start_loop function of the ColorMatcher module
#Part 2
#Declare a map that contains book ISBN as a key and book name as a value.
#Add at least 5 books into the map
#Create a loop that reads commands from the user:
#list lists all books in the map.
#search ISBN searches a book with specified ISBN and prints book info.
#add ISBN,NAME adds new book into the map.
#remove ISBN removes book with ISBN if found on map.
#quit exits the loop.
defmodule BookMap do #a module with a map of books within
@books %{
"ISBN1234567890" => "The Great Gatsby",
"ISBN0987654321" => "To Kill a Mockingbird",
"ISBN5678901234" => "1984",
"ISBN9876543210" => "Harry Potter and the Sorcerer's Stone",
"ISBN5432109876" => "The Catcher in the Rye"
}
def main do #loop function is called with the map of books given
loop(@books)
end
def loop(books) do #this loop works as a main that reads user's commands and executes them
IO.puts("Enter a command (list, search ISBN(give only number), add [ISBN] [NAME], remove ISBN(give only number), quit):")
command = String.downcase(IO.gets(""))
case String.split(command, ~r/\s+/, trim: true) do
["list"] -> list_books(books) #gives a list of the books
["search", raw_isbn] -> search_book(books, raw_isbn) #searches book with isbn number
["add" | rest] -> add_book(books, rest) #adds a new book into map
["remove", isbn] -> loop(Map.delete(books, "ISBN" <> isbn)) #removes book from map
["quit"] -> IO.puts("Goodbye!") #quits loop
_ -> IO.puts("Invalid command. Try again."); loop(books) #in case of invalid command
end
end
def list_books(books) do #function that lists books
IO.puts("List of Books:")
Enum.each(books, fn {isbn, name} -> IO.puts("#{isbn}: #{name}") end)
loop(books)
end
def search_book(books, raw_isbn) do #function that searches book
normalized_isbn = "ISBN" <> String.replace_prefix(raw_isbn, "ISBN", "") #to make search easier, prefix is replaced
case Map.get(books, normalized_isbn) do
nil -> IO.puts("Book not found.")
name -> IO.puts("Book found: #{normalized_isbn}: #{name}")
end
loop(books) #calling back to loop
end
defp add_book(books, [isbn | name_parts]) do #adds books to map
name = Enum.join(name_parts, " ")
loop(Map.put(books, "ISBN" <> isbn, name))
end
defp add_book(books, _) do
IO.puts("Invalid add command. Try again.")
loop(books) #calling back to loop
end
end
BookMap.main()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment