Posts

Showing posts from October, 2020

Greedy Algorithms

Image
A greedy algorithm is a simple, intuitive algorithm that is used in optimization problems. The algorithm makes the optimal choice at each step as it attempts to find the overall optimal way to solve the entire problem. Question 01 Sandun is a wine lover. He had to be in his home for three months due to the Covid-19 situation. Sandun wants to buy some wine since the curfew has been lifted. He goes to his favorite wine yard with some empty bottles. The wine yard owner is pleased to see Sandun. Hence, he offers Sandun free wine on one condition. Sandun can fill only the bottles he carries in his bag. Wine barrels in the wine yard have a tag in each one of them which has two values containing the number of bottles in the barrel and the price of the whole barrel. You have to build a program to help Sandun to take maximum valued wine from this offer. Given the number of bottles that he brought and the information on the wine barrels, you have to find the maximum value of wines Sandun gets fr

Functional Programming Using Scala

Image
Functional programming (often abbreviated FP) is the process of building software by composing pure functions, avoiding shared state, mutable data, and side-effects. Functional programming is declarative rather than imperative, and the application state flows through pure functions. Contrast with object-oriented programming, where the application state is usually shared and colocated with methods in objects. These are some examples of functional programming problems. Introduction 1. The temperature is 35C; convert this temperature into Fahrenheit.  ºF =ºC * 1.8000 + 32.00 def convert(x:Double):Double=x*1.8000+32.00 println(convert(35)) 2.The volume of a sphere with radius r is 4/3 Pi r3. What is the volume of a sphere with radius 5? def volume(x:Double):Double=4/3*math.Pi*x*x*x println(volume(5)) 3. Suppose the cover price of a book is Rs. 24.95, but bookstores get a 40% discount.  Shipping costs Rs. 3 for the first 50 copy and 75 cents for each additional copy.  What is the total whol