Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. Friday Programming Quiz [modified]

Friday Programming Quiz [modified]

Scheduled Pinned Locked Moved The Lounge
helptutorial
34 Posts 15 Posters 9 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D David Stone

    Here's an oddball language. Prolog. We did a similar solution involving restaurants and meals in my programming languages class a few quarters ago. Here's the adaptation for this problem. (I'm half betting/half hoping that nobody will actually plug this into a prolog interpreter...as I didn't actually check any of it. ;P This should work though.)

    item(Vodka).
    item(Rum).
    item(OneFiftyOneRum).
    item(Beer).
    item(Amaretto).
    item(Kahlua).
    item(SkimMilk).
    item(Mint).
    item(Sugar).
    item(CoconutJuice).
    item(PineappleJuice);
    item(Bourbon).

    drink(AnnaKournikova, [Vodka, Kahlua, SkimMilk]).
    drink(FlamingDrPepper, [Beer, OneFiftyOneRum, Amaretto]).
    drink(PinaColada, [Rum, CoconutJuice, PineappleJuice]).
    drink(MintJulep, [Mint, Bourbon, Sugar]).

    stock([Vodka, Rum, OneFiftyOneRum, Beer, Amaretto, Kahlua, SkimMilk, Mint, Sugar]).

    % Determines if the Drink is made with the given Ingredients
    canBeMade(Drink, Ingredients) :-
       drink(Drink, ActualIngredients),                          % Get the ingredients in the meal
       intersection(ActualIngredients, Ingredients, Intersect),  % Get the intersection of the actual ingredients with the specified ingredients
       ActualIngredients = Intersect.                            % Check that they're equal

    % Print a list
    print_list([]).
    print_list([H|T]) :- print(H), print_list(T).

    :- print_list(canBeMade(_, stock)).


    Last modified: 28mins after originally posted --

    We are certainly uncertain at least I'm pretty sure I am...

    R Offline
    R Offline
    Rama Krishna Vavilala
    wrote on last edited by
    #17

    David Stone wrote:

    oddball language. Prolog

    Hey! That's my second favorite language. (First one being Scheme).

    1 Reply Last reply
    0
    • R Rama Krishna Vavilala

      Given: 1. A list of cocktail recipes. Each recipe consists of ingredients . 2. A list of ingredients in stock The problem: Find all the cocktails in the list of cocktails that can be made using the ingredients in stock. Let's not consider the quantities in stock and assume that they are infinite. Example: Ingredients in stock: Vodka, Rum, 151 Rum, Beer, Amaretto, Kahlua, Skim Milk, Mint, Sugar Recipes: Anna Kournikova: Vodka, Kahlua, Skim Milk Flaming Dr Pepper: Beer, 151 Rum, Amaretto Pina Colada: Rum, Coconut Juice, Pineapple Juice Mint Julep: Mint, Bourbon, Sugar The output should be: Anna Kournikova, Flaming Dr Pepper -- modified at 17:57 Friday 11th May, 2007 Use a language of your choice and Data Structures of your choice -- modified at 19:06 Friday 11th May, 2007

      S Offline
      S Offline
      Shog9 0
      wrote on last edited by
      #18

      Here's my entry: http://www.shog9.com/drinks/drinks.html [^] (Firefox only. Kinda works in Opera, IE throws away newlines, which kinda defeats the purpose of including a parser) The code is terrible, and i freely admit it; also, it doesn't quite work the way you described it... but i like how it works, so i just kinda went with it. As a bonus, it also has the proper ingredient list for a mint julep... :rolleyes: Now, if you'll excuse me, all this coding has made me thirsty...

      ----

      i hope you are feeling sleepy for people not calling you by the same.

      --BarnaKol on abusive words

      S R S 3 Replies Last reply
      0
      • S Shog9 0

        Here's my entry: http://www.shog9.com/drinks/drinks.html [^] (Firefox only. Kinda works in Opera, IE throws away newlines, which kinda defeats the purpose of including a parser) The code is terrible, and i freely admit it; also, it doesn't quite work the way you described it... but i like how it works, so i just kinda went with it. As a bonus, it also has the proper ingredient list for a mint julep... :rolleyes: Now, if you'll excuse me, all this coding has made me thirsty...

        ----

        i hope you are feeling sleepy for people not calling you by the same.

        --BarnaKol on abusive words

        S Offline
        S Offline
        Sathesh Sakthivel
        wrote on last edited by
        #19

        Hey Shog, There is just a blank page. Seems you have coded in a drinking mood means there should be an error but nothing is there.

        Regards, Satips.

        S 1 Reply Last reply
        0
        • S Sathesh Sakthivel

          Hey Shog, There is just a blank page. Seems you have coded in a drinking mood means there should be an error but nothing is there.

          Regards, Satips.

          S Offline
          S Offline
          Shog9 0
          wrote on last edited by
          #20

          I didn't bother testing in IE... guess i'll have to give that wretched beast a look.

          ----

          i hope you are feeling sleepy for people not calling you by the same.

          --BarnaKol on abusive words

          1 Reply Last reply
          0
          • R Rama Krishna Vavilala

            Given: 1. A list of cocktail recipes. Each recipe consists of ingredients . 2. A list of ingredients in stock The problem: Find all the cocktails in the list of cocktails that can be made using the ingredients in stock. Let's not consider the quantities in stock and assume that they are infinite. Example: Ingredients in stock: Vodka, Rum, 151 Rum, Beer, Amaretto, Kahlua, Skim Milk, Mint, Sugar Recipes: Anna Kournikova: Vodka, Kahlua, Skim Milk Flaming Dr Pepper: Beer, 151 Rum, Amaretto Pina Colada: Rum, Coconut Juice, Pineapple Juice Mint Julep: Mint, Bourbon, Sugar The output should be: Anna Kournikova, Flaming Dr Pepper -- modified at 17:57 Friday 11th May, 2007 Use a language of your choice and Data Structures of your choice -- modified at 19:06 Friday 11th May, 2007

            T Offline
            T Offline
            Tomas Petricek
            wrote on last edited by
            #21

            Very nice quiz for functional languages :-). In F# it looks like this:

            let drinks = [
            ("Anna Kournikova", ["Vodka"; "Kahlua"; "Skim Milk"]);
            ("Flaming Dr Pepper", ["Beer"; "151 Rum"; "Amaretto"]);
            ("Pina Colada", ["Rum"; "Coconut Juice"; "Pineapple Juice"]);
            ("Mint Julep", ["Mint"; "Bourbon"; "Sugar"])];;
            let ingred =
            ["Vodka"; "Rum"; "151 Rum"; "Beer"; "Amaretto";
            "Kahlua"; "Skim Milk"; "Mint"; "Sugar"];;

            let ingset = Set.of_list ingred;;
            drinks |> List.filter (fun (_, ing) -> ing |> Seq.for_all ingset.Contains )
            |> List.map fst;;

            Homepage: TomasP.net | Photo of the month: Calendar | C# and LINQ, F#, Phalanger: My Blog
            Latest article: Phalanger, PHP for .NET: Introduction for .NET developers

            T M S 3 Replies Last reply
            0
            • T Tomas Petricek

              Very nice quiz for functional languages :-). In F# it looks like this:

              let drinks = [
              ("Anna Kournikova", ["Vodka"; "Kahlua"; "Skim Milk"]);
              ("Flaming Dr Pepper", ["Beer"; "151 Rum"; "Amaretto"]);
              ("Pina Colada", ["Rum"; "Coconut Juice"; "Pineapple Juice"]);
              ("Mint Julep", ["Mint"; "Bourbon"; "Sugar"])];;
              let ingred =
              ["Vodka"; "Rum"; "151 Rum"; "Beer"; "Amaretto";
              "Kahlua"; "Skim Milk"; "Mint"; "Sugar"];;

              let ingset = Set.of_list ingred;;
              drinks |> List.filter (fun (_, ing) -> ing |> Seq.for_all ingset.Contains )
              |> List.map fst;;

              Homepage: TomasP.net | Photo of the month: Calendar | C# and LINQ, F#, Phalanger: My Blog
              Latest article: Phalanger, PHP for .NET: Introduction for .NET developers

              T Offline
              T Offline
              Tomas Petricek
              wrote on last edited by
              #22

              ... or if you want just a one line version (excluding the data initialization), then it can be written like this:

              drinks |> List.filter (snd >> Seq.for_all ingset.Contains) |> List.map fst

              :rolleyes:

              Homepage: TomasP.net | Photo of the month: Calendar | C# and LINQ, F#, Phalanger: My Blog
              Latest article: Phalanger, PHP for .NET: Introduction for .NET developers

              1 Reply Last reply
              0
              • S Shog9 0

                Here's my entry: http://www.shog9.com/drinks/drinks.html [^] (Firefox only. Kinda works in Opera, IE throws away newlines, which kinda defeats the purpose of including a parser) The code is terrible, and i freely admit it; also, it doesn't quite work the way you described it... but i like how it works, so i just kinda went with it. As a bonus, it also has the proper ingredient list for a mint julep... :rolleyes: Now, if you'll excuse me, all this coding has made me thirsty...

                ----

                i hope you are feeling sleepy for people not calling you by the same.

                --BarnaKol on abusive words

                R Offline
                R Offline
                Rama Krishna Vavilala
                wrote on last edited by
                #23

                I love it!

                S 1 Reply Last reply
                0
                • R Rama Krishna Vavilala

                  I love it!

                  S Offline
                  S Offline
                  Shog9 0
                  wrote on last edited by
                  #24

                  Then something tells me you haven't looked at the code yet... ;P

                  ----

                  i hope you are feeling sleepy for people not calling you by the same.

                  --BarnaKol on abusive words

                  1 Reply Last reply
                  0
                  • T Tomas Petricek

                    Very nice quiz for functional languages :-). In F# it looks like this:

                    let drinks = [
                    ("Anna Kournikova", ["Vodka"; "Kahlua"; "Skim Milk"]);
                    ("Flaming Dr Pepper", ["Beer"; "151 Rum"; "Amaretto"]);
                    ("Pina Colada", ["Rum"; "Coconut Juice"; "Pineapple Juice"]);
                    ("Mint Julep", ["Mint"; "Bourbon"; "Sugar"])];;
                    let ingred =
                    ["Vodka"; "Rum"; "151 Rum"; "Beer"; "Amaretto";
                    "Kahlua"; "Skim Milk"; "Mint"; "Sugar"];;

                    let ingset = Set.of_list ingred;;
                    drinks |> List.filter (fun (_, ing) -> ing |> Seq.for_all ingset.Contains )
                    |> List.map fst;;

                    Homepage: TomasP.net | Photo of the month: Calendar | C# and LINQ, F#, Phalanger: My Blog
                    Latest article: Phalanger, PHP for .NET: Introduction for .NET developers

                    M Offline
                    M Offline
                    Miszou
                    wrote on last edited by
                    #25

                    X| I took functional programming as a module in the final year of my degree, 13 years ago. I thought it would be "interesting" compared to some of the other more essay-based modules. Hoo boy, it was interesting alright! We were using a language called Gopher IIRC. During the 3 hour final exam, most people (including myself) had left before the first hour was up, unable to answer most of the questions. There was an inquiry into the difficulty of the exam paper after this, and the papers were re-evaluated and marked up. It looked like an interesting idea, but I've yet to see anything useful written in it... As wikipedia[^] states, it finds most application in academia rather than commercial software development. And IMHO, it should stay in academia too - well away from me! ;P


                    Sunrise Wallpaper Project | The StartPage Randomizer | A Random Web Page

                    S T 2 Replies Last reply
                    0
                    • T Tomas Petricek

                      Very nice quiz for functional languages :-). In F# it looks like this:

                      let drinks = [
                      ("Anna Kournikova", ["Vodka"; "Kahlua"; "Skim Milk"]);
                      ("Flaming Dr Pepper", ["Beer"; "151 Rum"; "Amaretto"]);
                      ("Pina Colada", ["Rum"; "Coconut Juice"; "Pineapple Juice"]);
                      ("Mint Julep", ["Mint"; "Bourbon"; "Sugar"])];;
                      let ingred =
                      ["Vodka"; "Rum"; "151 Rum"; "Beer"; "Amaretto";
                      "Kahlua"; "Skim Milk"; "Mint"; "Sugar"];;

                      let ingset = Set.of_list ingred;;
                      drinks |> List.filter (fun (_, ing) -> ing |> Seq.for_all ingset.Contains )
                      |> List.map fst;;

                      Homepage: TomasP.net | Photo of the month: Calendar | C# and LINQ, F#, Phalanger: My Blog
                      Latest article: Phalanger, PHP for .NET: Introduction for .NET developers

                      S Offline
                      S Offline
                      Stuart Dootson
                      wrote on last edited by
                      #26

                      Tomas Petricek wrote:

                      Very nice quiz for functional languages

                      I agree - I used Haskell rather than F# (and algebraic data types rather than just lists):

                      module Cocktails where
                      
                      ingredients = ["Vodka", "Rum", "151 Rum", "Beer", "Amaretto", "Kahlua", "Skim Milk", "Mint", "Sugar"]
                      
                      data Cocktail = Recipe { name::String, uses::[String]} deriving (Show)
                      
                      recipes = [ Recipe "Anna Kournikova" ["Vodka", "Kahlua", "Skim Milk"],
                                  Recipe "Flaming Dr Pepper" ["Beer", "151 Rum", "Amaretto"],
                                  Recipe "Pina Colada" ["Rum", "Coconut Juice", "Pineapple Juice"],
                                  Recipe "Mint Julep" ["Mint", "Bourbon", "Sugar"]]
                      
                      haveIngredients i (Recipe _ uses) = all (`elem` i)  uses
                      
                      canMake recipes ingredients = map name $ filter (haveIngredients ingredients) recipes
                      

                      Mmmmmm - Haskell :)

                      1 Reply Last reply
                      0
                      • M Miszou

                        X| I took functional programming as a module in the final year of my degree, 13 years ago. I thought it would be "interesting" compared to some of the other more essay-based modules. Hoo boy, it was interesting alright! We were using a language called Gopher IIRC. During the 3 hour final exam, most people (including myself) had left before the first hour was up, unable to answer most of the questions. There was an inquiry into the difficulty of the exam paper after this, and the papers were re-evaluated and marked up. It looked like an interesting idea, but I've yet to see anything useful written in it... As wikipedia[^] states, it finds most application in academia rather than commercial software development. And IMHO, it should stay in academia too - well away from me! ;P


                        Sunrise Wallpaper Project | The StartPage Randomizer | A Random Web Page

                        S Offline
                        S Offline
                        Stuart Dootson
                        wrote on last edited by
                        #27

                        The basic idea of functional programming (i.e. functions are first class types) is incredibly useful. Add currying and closures into the mix and you have something even more incredibly useful. The tricky thing with most functional languages is the immutability of data, which is what gives referential transparency (which is useful for other reasons) - however, that's not necessary for getting at least some of the benefits of FP. C++ with Boost.Function[^] and Boost.Lambda[^] does the job for me when I'm at work.

                        1 Reply Last reply
                        0
                        • D David Stone

                          Here's an oddball language. Prolog. We did a similar solution involving restaurants and meals in my programming languages class a few quarters ago. Here's the adaptation for this problem. (I'm half betting/half hoping that nobody will actually plug this into a prolog interpreter...as I didn't actually check any of it. ;P This should work though.)

                          item(Vodka).
                          item(Rum).
                          item(OneFiftyOneRum).
                          item(Beer).
                          item(Amaretto).
                          item(Kahlua).
                          item(SkimMilk).
                          item(Mint).
                          item(Sugar).
                          item(CoconutJuice).
                          item(PineappleJuice);
                          item(Bourbon).

                          drink(AnnaKournikova, [Vodka, Kahlua, SkimMilk]).
                          drink(FlamingDrPepper, [Beer, OneFiftyOneRum, Amaretto]).
                          drink(PinaColada, [Rum, CoconutJuice, PineappleJuice]).
                          drink(MintJulep, [Mint, Bourbon, Sugar]).

                          stock([Vodka, Rum, OneFiftyOneRum, Beer, Amaretto, Kahlua, SkimMilk, Mint, Sugar]).

                          % Determines if the Drink is made with the given Ingredients
                          canBeMade(Drink, Ingredients) :-
                             drink(Drink, ActualIngredients),                          % Get the ingredients in the meal
                             intersection(ActualIngredients, Ingredients, Intersect),  % Get the intersection of the actual ingredients with the specified ingredients
                             ActualIngredients = Intersect.                            % Check that they're equal

                          % Print a list
                          print_list([]).
                          print_list([H|T]) :- print(H), print_list(T).

                          :- print_list(canBeMade(_, stock)).


                          Last modified: 28mins after originally posted --

                          We are certainly uncertain at least I'm pretty sure I am...

                          S Offline
                          S Offline
                          Stuart Dootson
                          wrote on last edited by
                          #28

                          David Stone wrote:

                          % Print a list
                          print_list([]).
                          print_list([H|T]) :- print(H), print_list(T).
                          

                          Does Prolog not have a map function?

                          D 1 Reply Last reply
                          0
                          • M Miszou

                            X| I took functional programming as a module in the final year of my degree, 13 years ago. I thought it would be "interesting" compared to some of the other more essay-based modules. Hoo boy, it was interesting alright! We were using a language called Gopher IIRC. During the 3 hour final exam, most people (including myself) had left before the first hour was up, unable to answer most of the questions. There was an inquiry into the difficulty of the exam paper after this, and the papers were re-evaluated and marked up. It looked like an interesting idea, but I've yet to see anything useful written in it... As wikipedia[^] states, it finds most application in academia rather than commercial software development. And IMHO, it should stay in academia too - well away from me! ;P


                            Sunrise Wallpaper Project | The StartPage Randomizer | A Random Web Page

                            T Offline
                            T Offline
                            Tomas Petricek
                            wrote on last edited by
                            #29

                            I admit that my code may look a bit scary because I used a few "advanced" functional tricks, but once you get the idea than it is easy to understand and easy to write. When I attended my functional programming class I didn't understand anythin either and I wasn't very interested in playing with it to learn it because it is quite difficult to do anything fancy in Haskell, but than I started playing with F# which is a lot of fun because you can use everything from .NET so it's very easy to start... Anyway, I think that functional programming is slowly getting into a real world - look at C# 3.0 or Python - they both have quite a lot functional concepts in them...

                            Homepage: TomasP.net | Photo of the month: Calendar | C# and LINQ, F#, Phalanger: My Blog
                            Latest article: Phalanger, PHP for .NET: Introduction for .NET developers

                            1 Reply Last reply
                            0
                            • S Stuart Dootson

                              David Stone wrote:

                              % Print a list
                              print_list([]).
                              print_list([H|T]) :- print(H), print_list(T).
                              

                              Does Prolog not have a map function?

                              D Offline
                              D Offline
                              David Stone
                              wrote on last edited by
                              #30

                              I'm sure it does...but we weren't allowed to use it in the programming languages class I was in. Since I just copied the code wholesale, I figured I'd include that. ;)

                              We are certainly uncertain at least I'm pretty sure I am...

                              S 1 Reply Last reply
                              0
                              • D David Stone

                                I'm sure it does...but we weren't allowed to use it in the programming languages class I was in. Since I just copied the code wholesale, I figured I'd include that. ;)

                                We are certainly uncertain at least I'm pretty sure I am...

                                S Offline
                                S Offline
                                Stuart Dootson
                                wrote on last edited by
                                #31

                                David Stone wrote:

                                Since I just copied the code wholesale, I figured I'd include that.

                                Hey - code reuse - can't criticise that :)

                                1 Reply Last reply
                                0
                                • S Shog9 0

                                  Here's my entry: http://www.shog9.com/drinks/drinks.html [^] (Firefox only. Kinda works in Opera, IE throws away newlines, which kinda defeats the purpose of including a parser) The code is terrible, and i freely admit it; also, it doesn't quite work the way you described it... but i like how it works, so i just kinda went with it. As a bonus, it also has the proper ingredient list for a mint julep... :rolleyes: Now, if you'll excuse me, all this coding has made me thirsty...

                                  ----

                                  i hope you are feeling sleepy for people not calling you by the same.

                                  --BarnaKol on abusive words

                                  S Offline
                                  S Offline
                                  S Senthil Kumar
                                  wrote on last edited by
                                  #32

                                  I like the way it displays potential recipes, the way it uses color/font weights to differentiate recipes and best of it all, the way it sorts recipes by ingredient availability. I love it :)

                                  Regards Senthil [MVP - Visual C#] _____________________________ My Blog | My Articles | My Flickr | WinMacro

                                  S 1 Reply Last reply
                                  0
                                  • S S Senthil Kumar

                                    I like the way it displays potential recipes, the way it uses color/font weights to differentiate recipes and best of it all, the way it sorts recipes by ingredient availability. I love it :)

                                    Regards Senthil [MVP - Visual C#] _____________________________ My Blog | My Articles | My Flickr | WinMacro

                                    S Offline
                                    S Offline
                                    Shog9 0
                                    wrote on last edited by
                                    #33

                                    Thanks. :)

                                    ----

                                    i hope you are feeling sleepy for people not calling you by the same.

                                    --BarnaKol on abusive words

                                    1 Reply Last reply
                                    0
                                    • C Christian Graus

                                      Assuming that low a number of ingredients, you could write code that assigns a bit to each ingredient, and a field with the bits set for the ingredients that each cocktail needs. Then you can very simply work out which ones you have the ingredients for, without having to write much code at all. Anna Kournikova is a cocktail ? Yummy.

                                      Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

                                      E Offline
                                      E Offline
                                      ednrgc
                                      wrote on last edited by
                                      #34

                                      Christian Graus wrote:

                                      Anna Kournikova is a cocktail ? Yummy.

                                      If Danica Patrick doesn't win soon, will she eclipse Anna as the "Hype but never came close to winning" queen?

                                      1 Reply Last reply
                                      0
                                      Reply
                                      • Reply as topic
                                      Log in to reply
                                      • Oldest to Newest
                                      • Newest to Oldest
                                      • Most Votes


                                      • Login

                                      • Don't have an account? Register

                                      • Login or register to search.
                                      • First post
                                        Last post
                                      0
                                      • Categories
                                      • Recent
                                      • Tags
                                      • Popular
                                      • World
                                      • Users
                                      • Groups