Friday Programming Quiz
-
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
12Full marks to a VB.NET 9.0 solution.;)
Co-Author ASP.NET AJAX in Action
-
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
12Full marks to a VB.NET 9.0 solution.;)
Co-Author ASP.NET AJAX in Action
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 SubHomepage: TomasP.net | Photo of the month: Calendar | C# and LINQ, F#, Phalanger: My Blog
Latest article: Phalanger, PHP for .NET: Introduction for .NET developers -
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 SubHomepage: TomasP.net | Photo of the month: Calendar | C# and LINQ, F#, Phalanger: My Blog
Latest article: Phalanger, PHP for .NET: Introduction for .NET developersTomas 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
-
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
12Full marks to a VB.NET 9.0 solution.;)
Co-Author ASP.NET AJAX in Action
@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
-
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
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 -
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
12Full marks to a VB.NET 9.0 solution.;)
Co-Author ASP.NET AJAX in Action
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
-
@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
Hot damn, somebody who likes to do perverted things with batch files as much as I do! :cool: :-D
Software Zen:
delete this;
-
@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
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^**
-
Hot damn, somebody who likes to do perverted things with batch files as much as I do! :cool: :-D
Software Zen:
delete this;
Wearing your black waders I hope.
-
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
12Full marks to a VB.NET 9.0 solution.;)
Co-Author ASP.NET AJAX in Action
Care to enlighten us as to why you've had to do that?
-
Care to enlighten us as to why you've had to do that?
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
-
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
Yeah, but that's still in beta, right? :-D
-
Yeah, but that's still in beta, right? :-D
-
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:
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#.
-
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#.
VB is the next MS dynamic language.
-
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 SubHomepage: TomasP.net | Photo of the month: Calendar | C# and LINQ, F#, Phalanger: My Blog
Latest article: Phalanger, PHP for .NET: Introduction for .NET developers -
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
12Full marks to a VB.NET 9.0 solution.;)
Co-Author ASP.NET AJAX in Action
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[^]
-
Hot damn, somebody who likes to do perverted things with batch files as much as I do! :cool: :-D
Software Zen:
delete this;
It is really amazing to see a charming quick cut batch file doing that marvellously. :)
Vasudevan Deepak Kumar Personal Homepage Tech Gossips
-
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^**
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
-
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
12Full marks to a VB.NET 9.0 solution.;)
Co-Author ASP.NET AJAX in Action
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