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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. The Lounge
  3. Friday Programming Quiz

Friday Programming Quiz

Scheduled Pinned Locked Moved The Lounge
csharpasp-netcomtutorial
24 Posts 15 Posters 3 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.
  • R Rama Krishna Vavilala

    Given a set of numbers passed to the command line. The number of number is always odd. The program should sort them and output the 5 numbers in the middle. For example, if there are 9 numbers the output should be sorted numbers from indices 2 to 6. e.g.

    c:> med5 67 89 12 1 8 1 3 5 7
    3
    5
    7
    8
    12

    Full marks to a VB.NET 9.0 solution.;)

    Co-Author ASP.NET AJAX in Action

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

    @echo off set /a count=1 :: Load all the numbers :loop echo %1 > _%count%.var set /a count=count+1 shift if "%1" == "" goto :startSort goto loop :startSort set /a total=count-1 :RestartSort set /a count=1 :sortLoop set /a next=%count%+1 call :swap %count% %next% set /a count=count+1 if "%swapped%" == "true" goto :RestartSort if "%count%" == "%total%" goto :output goto :sortLoop :swap set /P var1="" < _%1.var set /P var2="" < _%2.var if /I %var1% LEQ %var2% goto noSwap ren _%1.var _temp.var ren _%2.var _%1.var ren _temp.var _%2.var set swapped=true goto :eof :noSwap set swapped= goto :eof :output set /A offset=((%total%-5)/2)+1 set /A limit=%offset%+4 for /L %%i in (%offset%,1,%limit%) do call :showval %%i :cleanup erase *.var set next= set offset= set total= set count= set var= set var1= set var2= goto :eof


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

    G L 2 Replies Last reply
    0
    • M Marc Clifton

      Tomas Petricek wrote:

      For Each n In (From a In args Select r = Int32.Parse(a) Order By r Skip (args.Length - 5) / 2 Take 5)

      That's both impressive and scary. (And not that it's VB, but what you can do with Linq) Marc

      Thyme In The Country
      Interacx
      My Blog

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

      I think part of the scariness is the VB syntax for queries. VB queries contain almost anything you may ever need (I was quite suprised when I tried it :-)). That's the VB style of doing things - it makes programming easier, but you can easily write really ugly and inefficient code. The C# queries are slightly better (they don't direcly support 'Skip' and 'Take'), but in general the simplicity still hides a lot from what is actually going on. I think that when the same thing is written using the new language features (general purpose features, so no queries!), it doesn't look that scary. It will be a bit longer though (I'm switching back to C#, since I'm not sure how VB does anonymous functions - though I expect that it will be uglier than in C#):

      // It would look roughly like this..
      args.Select(a => Int32.Parse(a).SortBy(a => a).Skip((args.Length - 5) / 2).Take(5)

      Using this syntax it is much easier to see what the code actually does - It's sill not perfect, but it's probably the best that can be done in C#/VB-like language. In functional languages (e.g. in my favorite F# for .NET) solutions like this are more natural (once you learn the language basics), because supporting code like this was the goal of the language design from the beginning.

      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
      • M Miszou

        @echo off set /a count=1 :: Load all the numbers :loop echo %1 > _%count%.var set /a count=count+1 shift if "%1" == "" goto :startSort goto loop :startSort set /a total=count-1 :RestartSort set /a count=1 :sortLoop set /a next=%count%+1 call :swap %count% %next% set /a count=count+1 if "%swapped%" == "true" goto :RestartSort if "%count%" == "%total%" goto :output goto :sortLoop :swap set /P var1="" < _%1.var set /P var2="" < _%2.var if /I %var1% LEQ %var2% goto noSwap ren _%1.var _temp.var ren _%2.var _%1.var ren _temp.var _%2.var set swapped=true goto :eof :noSwap set swapped= goto :eof :output set /A offset=((%total%-5)/2)+1 set /A limit=%offset%+4 for /L %%i in (%offset%,1,%limit%) do call :showval %%i :cleanup erase *.var set next= set offset= set total= set count= set var= set var1= set var2= goto :eof


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

        G Offline
        G Offline
        Gary R Wheeler
        wrote on last edited by
        #6

        Hot damn, somebody who likes to do perverted things with batch files as much as I do! :cool: :-D


        Software Zen: delete this;

        Fold With Us![^]

        P V 2 Replies Last reply
        0
        • R Rama Krishna Vavilala

          Given a set of numbers passed to the command line. The number of number is always odd. The program should sort them and output the 5 numbers in the middle. For example, if there are 9 numbers the output should be sorted numbers from indices 2 to 6. e.g.

          c:> med5 67 89 12 1 8 1 3 5 7
          3
          5
          7
          8
          12

          Full marks to a VB.NET 9.0 solution.;)

          Co-Author ASP.NET AJAX in Action

          M Offline
          M Offline
          Michael Dunn
          wrote on last edited by
          #7

          print join "\n", @{[sort {$a <=> $b} @ARGV]}[$#ARGV/2-2 .. $#ARGV/2+2];

          --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ Hungarian notation FTW

          1 Reply Last reply
          0
          • M Miszou

            @echo off set /a count=1 :: Load all the numbers :loop echo %1 > _%count%.var set /a count=count+1 shift if "%1" == "" goto :startSort goto loop :startSort set /a total=count-1 :RestartSort set /a count=1 :sortLoop set /a next=%count%+1 call :swap %count% %next% set /a count=count+1 if "%swapped%" == "true" goto :RestartSort if "%count%" == "%total%" goto :output goto :sortLoop :swap set /P var1="" < _%1.var set /P var2="" < _%2.var if /I %var1% LEQ %var2% goto noSwap ren _%1.var _temp.var ren _%2.var _%1.var ren _temp.var _%2.var set swapped=true goto :eof :noSwap set swapped= goto :eof :output set /A offset=((%total%-5)/2)+1 set /A limit=%offset%+4 for /L %%i in (%offset%,1,%limit%) do call :showval %%i :cleanup erase *.var set next= set offset= set total= set count= set var= set var1= set var2= goto :eof


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

            L Offline
            L Offline
            leppie
            wrote on last edited by
            #8

            And the GWBASIC one? ;P

            **

            xacc.ide-0.2.0.77 - now with C# 3.5 support and Navigation Bar!^
            New xacc.ide release RSS feed^

            **

            V 1 Reply Last reply
            0
            • G Gary R Wheeler

              Hot damn, somebody who likes to do perverted things with batch files as much as I do! :cool: :-D


              Software Zen: delete this;

              Fold With Us![^]

              P Offline
              P Offline
              PIEBALDconsult
              wrote on last edited by
              #9

              Wearing your black waders I hope.

              1 Reply Last reply
              0
              • R Rama Krishna Vavilala

                Given a set of numbers passed to the command line. The number of number is always odd. The program should sort them and output the 5 numbers in the middle. For example, if there are 9 numbers the output should be sorted numbers from indices 2 to 6. e.g.

                c:> med5 67 89 12 1 8 1 3 5 7
                3
                5
                7
                8
                12

                Full marks to a VB.NET 9.0 solution.;)

                Co-Author ASP.NET AJAX in Action

                P Offline
                P Offline
                PIEBALDconsult
                wrote on last edited by
                #10

                Care to enlighten us as to why you've had to do that?

                R 1 Reply Last reply
                0
                • P PIEBALDconsult

                  Care to enlighten us as to why you've had to do that?

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

                  After completing a LINQ book (which I was reviewing), I found out that LINQ in VB has more features than in C# (query expression wise). I was thinking about a simple example that can be used to demo LINQ in VB and that was it.

                  Co-Author ASP.NET AJAX in Action

                  P 1 Reply Last reply
                  0
                  • R Rama Krishna Vavilala

                    After completing a LINQ book (which I was reviewing), I found out that LINQ in VB has more features than in C# (query expression wise). I was thinking about a simple example that can be used to demo LINQ in VB and that was it.

                    Co-Author ASP.NET AJAX in Action

                    P Offline
                    P Offline
                    PIEBALDconsult
                    wrote on last edited by
                    #12

                    Yeah, but that's still in beta, right? :-D

                    C 1 Reply Last reply
                    0
                    • P PIEBALDconsult

                      Yeah, but that's still in beta, right? :-D

                      C Offline
                      C Offline
                      Clickok
                      wrote on last edited by
                      #13

                      PIEBALDconsult wrote:

                      Yeah, but that's still in beta, right? :-D

                      VB9 = Visual Beta 9 X|:-D


                      For God so loved the world, that he gave his only begotten Son, that whosoever believeth in him should not perish, but have everlasting life.(John 3:16) :badger:

                      P 1 Reply Last reply
                      0
                      • C Clickok

                        PIEBALDconsult wrote:

                        Yeah, but that's still in beta, right? :-D

                        VB9 = Visual Beta 9 X|:-D


                        For God so loved the world, that he gave his only begotten Son, that whosoever believeth in him should not perish, but have everlasting life.(John 3:16) :badger:

                        P Offline
                        P Offline
                        PIEBALDconsult
                        wrote on last edited by
                        #14

                        Not Vaporware Beta? :-D As VB is just a "prototyping language", it makes sense to use it as a test bed for Linq, before putting the final implementation in C#.

                        B B 2 Replies Last reply
                        0
                        • P PIEBALDconsult

                          Not Vaporware Beta? :-D As VB is just a "prototyping language", it makes sense to use it as a test bed for Linq, before putting the final implementation in C#.

                          B Offline
                          B Offline
                          Brady Kelly
                          wrote on last edited by
                          #15

                          VB is the next MS dynamic language.

                          1 Reply Last reply
                          0
                          • T Tomas Petricek

                            Rama Krishna Vavilala wrote:

                            Full marks to a VB.NET 9.0 solution.

                            Now, that's a challenge!! I haven't used VB for at least 5 years, but here we go...

                            Sub Main(ByVal args() As String)
                            For Each n In (From a In args Select r = Int32.Parse(a) Order By r Skip (args.Length - 5) / 2 Take 5)
                            Console.WriteLine(n)
                            Next
                            End Sub

                            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

                            W Offline
                            W Offline
                            WillemM
                            wrote on last edited by
                            #16

                            Linq rules :D

                            WM. What about weapons of mass-construction? "What? Its an Apple MacBook Pro. They are sexy!" - Paul Watson My blog

                            1 Reply Last reply
                            0
                            • R Rama Krishna Vavilala

                              Given a set of numbers passed to the command line. The number of number is always odd. The program should sort them and output the 5 numbers in the middle. For example, if there are 9 numbers the output should be sorted numbers from indices 2 to 6. e.g.

                              c:> med5 67 89 12 1 8 1 3 5 7
                              3
                              5
                              7
                              8
                              12

                              Full marks to a VB.NET 9.0 solution.;)

                              Co-Author ASP.NET AJAX in Action

                              E Offline
                              E Offline
                              Eytukan
                              wrote on last edited by
                              #17
                              vector<int> vec_num;
                              for(int i=1;i<argc;i++){
                               vec_num.push_back(atoi((char*)argv[i]));
                              }
                              sort(vec_num.begin(),vec_num.end());
                              copy(vec_num.begin()+((vec_num.size()-5)/2),
                              vec_num.end()-((vec_num.size()-5)/2), ostream_iterator<int>(cout, "\n"));
                              

                              The Advantage in work-from-home is that... we can blame the dog -Mark Salsbery Best wishes to Rexx[^]

                              R 1 Reply Last reply
                              0
                              • G Gary R Wheeler

                                Hot damn, somebody who likes to do perverted things with batch files as much as I do! :cool: :-D


                                Software Zen: delete this;

                                Fold With Us![^]

                                V Offline
                                V Offline
                                Vasudevan Deepak Kumar
                                wrote on last edited by
                                #18

                                It is really amazing to see a charming quick cut batch file doing that marvellously. :)

                                Vasudevan Deepak Kumar Personal Homepage Tech Gossips

                                1 Reply Last reply
                                0
                                • L leppie

                                  And the GWBASIC one? ;P

                                  **

                                  xacc.ide-0.2.0.77 - now with C# 3.5 support and Navigation Bar!^
                                  New xacc.ide release RSS feed^

                                  **

                                  V Offline
                                  V Offline
                                  Vasudevan Deepak Kumar
                                  wrote on last edited by
                                  #19

                                  leppie wrote:

                                  GWBASIC

                                  Is it available for download somewhere? I got a copy of QBasic anyway from here: http://www.uv.tietgen.dk/staff/mlha/Download/DOS/microsoft/qbasic.exe[^]

                                  Vasudevan Deepak Kumar Personal Homepage Tech Gossips

                                  1 Reply Last reply
                                  0
                                  • R Rama Krishna Vavilala

                                    Given a set of numbers passed to the command line. The number of number is always odd. The program should sort them and output the 5 numbers in the middle. For example, if there are 9 numbers the output should be sorted numbers from indices 2 to 6. e.g.

                                    c:> med5 67 89 12 1 8 1 3 5 7
                                    3
                                    5
                                    7
                                    8
                                    12

                                    Full marks to a VB.NET 9.0 solution.;)

                                    Co-Author ASP.NET AJAX in Action

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

                                    Haskell

                                    module Main where
                                    
                                    import Data.List
                                    import System
                                    
                                    main = do nums <- fmap (map (read :: String -> Integer)) getArgs
                                              (print.take 5.drop (((length nums)-5) `div` 2).sort) nums
                                    
                                    1 Reply Last reply
                                    0
                                    • E Eytukan
                                      vector<int> vec_num;
                                      for(int i=1;i<argc;i++){
                                       vec_num.push_back(atoi((char*)argv[i]));
                                      }
                                      sort(vec_num.begin(),vec_num.end());
                                      copy(vec_num.begin()+((vec_num.size()-5)/2),
                                      vec_num.end()-((vec_num.size()-5)/2), ostream_iterator<int>(cout, "\n"));
                                      

                                      The Advantage in work-from-home is that... we can blame the dog -Mark Salsbery Best wishes to Rexx[^]

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

                                      5 for using the ostream_iterator alone:)

                                      Co-Author ASP.NET AJAX in Action

                                      E 1 Reply Last reply
                                      0
                                      • R Rama Krishna Vavilala

                                        5 for using the ostream_iterator alone:)

                                        Co-Author ASP.NET AJAX in Action

                                        E Offline
                                        E Offline
                                        Eytukan
                                        wrote on last edited by
                                        #22

                                        Anyway, a 5 is a 5 ;P. Btw I think I should learn a new language to reply to your Friday Question. Replies representing all other languages are just there in seconds you post your question.we can't make our replies unique unless I try for a deliberately different technique of the same language.:-D


                                        The Advantage in work-from-home is that... we can blame the dog -Mark Salsbery Best wishes to Rexx[^]

                                        1 Reply Last reply
                                        0
                                        • R Rama Krishna Vavilala

                                          Given a set of numbers passed to the command line. The number of number is always odd. The program should sort them and output the 5 numbers in the middle. For example, if there are 9 numbers the output should be sorted numbers from indices 2 to 6. e.g.

                                          c:> med5 67 89 12 1 8 1 3 5 7
                                          3
                                          5
                                          7
                                          8
                                          12

                                          Full marks to a VB.NET 9.0 solution.;)

                                          Co-Author ASP.NET AJAX in Action

                                          P Offline
                                          P Offline
                                          PIEBALDconsult
                                          wrote on last edited by
                                          #23

                                          Here's some C#.

                                          System.Collections.Generic.List<int> list = new System.Collections.Generic.List<int>
                                          (
                                          System.Array.ConvertAll<string,int>
                                          (
                                          args
                                          ,
                                          delegate ( string s ) { return ( (int) System.Convert.ChangeType ( s , typeof(int) ) ) ; }
                                          )
                                          ) ;

                                          list.Sort() ;

                                          foreach
                                          (
                                          int i
                                          in
                                          list.GetRange
                                          (
                                          args.Length/2-2
                                          ,
                                          5
                                          )
                                          )
                                          {
                                          System.Console.WriteLine ( i ) ;
                                          }

                                          This points up two shortcomings of the current (V2) implementation of .net: 1) System.Array doesn't have a GetRange() method 2) Sort() returns void -- returning a reference to the object would be more flexible If these shortcomings were corrected, the above code could be:

                                          foreach
                                          (
                                          int i
                                          in
                                          System.Array.ConvertAll<string,int>
                                          (
                                          args
                                          ,
                                          delegate ( string s ) { return ( (int) System.Convert.ChangeType ( s , typeof(int) ) ) ; }
                                          ).Sort().GetRange
                                          (
                                          args.Length/2-2
                                          ,
                                          5
                                          )
                                          )
                                          {
                                          System.Console.WriteLine ( i ) ;
                                          }

                                          -- modified at 16:33 Saturday 1st September, 2007 Whoops, forgot to protect the angle-brackets -- modified at 16:34 Saturday 1st September, 2007 And then of course I put them backward

                                          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