UPSB v4

Off-topic / A Programming Challenge

  1. Mats
    Date: Thu, Jan 12 2012 23:07:32

    So the task is as follows: Write a program that can do this: -The user can input up to a maximum of 100 positive integers. -The program then sorts them into order low to high and prints them to screen. -The program then tells you which of the numbers are prime. -Max integer size is 2^32 - 1 Any questions, ask in this thread. That's it. Go! (oh and put your answer in a spoiler if you do manage this)

  2. Advecticity
    Date: Fri, Jan 13 2012 00:09:33

    This is written in C# and definitely won't amaze anymore in terms of efficiency, but it's the quickest implementation I can think of, but it's only 100 numbers so whatever.

    Spoiler[CODE] using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace MatsChallenge { class Program { static void Main(string[] args) { List numbers = new List(); List sorted = new List(); Console.WriteLine("Please enter a positive integers smaller than 2^32."); Console.WriteLine("Any invalid input will assume you do not wish to enter anymore numbers"); // Input the 100 numbers. while (numbers.Count < 100) { Console.WriteLine("You can still enter up to {0} number(s).", 100 - numbers.Count); string input = Console.ReadLine(); try { uint number = uint.Parse(input); numbers.Add(number); } catch { break; } } if (numbers.Count > 0) Console.WriteLine("You entered {0} numbers."); else { Console.WriteLine("No numbers entered, can't do anything."); Console.ReadLine(); return; } // Sort the numbers. while (numbers.Count > 0) { uint smallest = uint.MaxValue; foreach (uint i in numbers) { if (i < smallest) smallest = i; } sorted.Add(smallest); numbers.Remove(smallest); } // Display every number as well as whether they are prime. foreach (uint i in sorted) { bool prime = true; for (int d = 2; d <= (int)Math.Sqrt(uint.MaxValue) + 1; d++) { if (i % d == 0 && i != d) { prime = false; break; } } if (prime) Console.WriteLine("{0} : Prime", i); else Console.WriteLine("{0} : Not Prime", i); } Console.ReadLine(); } } } [/CODE]

  3. nateiskewl
    Date: Fri, Jan 13 2012 00:14:50

    http://www.zombo.com/

  4. GeeGeeGee
    Date: Fri, Jan 13 2012 00:30:58

    Advecticity wrote: This is written in C# and definitely won't amaze anymore in terms of efficiency, but it's the quickest implementation I can think of, but it's only 100 numbers so whatever.
    Spoiler[CODE] using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace MatsChallenge { class Program { static void Main(string[] args) { List numbers = new List(); List sorted = new List(); Console.WriteLine("Please enter a positive integers smaller than 2^32."); Console.WriteLine("Any invalid input will assume you do not wish to enter anymore numbers"); // Input the 100 numbers. while (numbers.Count < 100) { Console.WriteLine("You can still enter up to {0} number(s).", 100 - numbers.Count); string input = Console.ReadLine(); try { uint number = uint.Parse(input); numbers.Add(number); } catch { break; } } if (numbers.Count > 0) Console.WriteLine("You entered {0} numbers."); else { Console.WriteLine("No numbers entered, can't do anything."); Console.ReadLine(); return; } // Sort the numbers. while (numbers.Count > 0) { uint smallest = uint.MaxValue; foreach (uint i in numbers) { if (i < smallest) smallest = i; } sorted.Add(smallest); numbers.Remove(smallest); } // Display every number as well as whether they are prime. foreach (uint i in sorted) { bool prime = true; for (int d = 2; d <= (int)Math.Sqrt(uint.MaxValue) + 1; d++) { if (i % d == 0 && i != d) { prime = false; break; } } if (prime) Console.WriteLine("{0} : Prime", i); else Console.WriteLine("{0} : Not Prime", i); } Console.ReadLine(); } } } [/CODE]
    i'm taking programming for the first time. this looks like a foreign language to me....fuck i feel stupid.

  5. Mats
    Date: Fri, Jan 13 2012 00:39:02

    GeeGeeGee wrote: i'm taking programming for the first time. this looks like a foreign language to me....fuck i feel stupid.
    I don't ever write in C#, believe me, reading that is hardly a walk in the park for me, although reading carefully, I can see what is happening. You will learn as a programmer, you need to learn a few general things or a few languages to read other stuff. Otherwise, you will only feel comfortable reading in *your* language.

  6. Furloy
    Date: Fri, Jan 13 2012 02:56:38

    I took an HTML website making class once.

  7. Advecticity
    Date: Fri, Jan 13 2012 03:03:23

    @GeeGeeGee C# has a lot of features built in, libraries to call and so on. It's also 100% object-oriented, which means even for a simple program you have to write a bunch of technicalities like the stuff below. [CODE] using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace MatsChallenge { class Program { static void Main(string[] args) { [/CODE] I didn't even write that, it's generated for me. Although List is just a dynamic array of integers (and array whose size can change as you add more elements), if I never used C# I wouldn't guess it (especially if I haven't learned arrays yet). Stuff like try { } catch {} are again, convenient features of the language that you wouldn't in something more straight forward, like C. Can't guess what they do, but it doesn't take long to learn how to use them. Don't worry, after one programming class you'd only need a few days of learning C# to understand all that code.

  8. nateiskewl
    Date: Fri, Jan 13 2012 03:39:04

    I posted a legitimate answer a few hours ago but for some reason it was deleted.

    Spoilerhttp://www.zombo.com/

  9. webspider
    Date: Fri, Jan 13 2012 03:54:06

    [SPOILER="Python3"] [CODE]from math import sqrt def isprime(n): if n < 2: return False for i in range(2, int(sqrt(n)) + 1): if n % i == 0: return False return True print("Please input up to OVER NYANTHOUSAND positive integers, seperated by single spaces") input = sorted(input("").split(" ")) print("Sorted:", " ".join(input)) print("Primes:", " ".join([i for i in input if i.isdigit() and isprime(int(i))]))[/CODE][/SPOILER] Seriously, what did you guys expect :hah: edit: May I ask you what's that challenging about this exercise?

  10. Mats
    Date: Fri, Jan 13 2012 09:08:53

    edit: May I ask you what's that challenging about this exercise?
    It's supposed to be a simple programming challenge. It just came from a discussion in shoutbox in which I bet if I put a simple programming excersize on UPSB, less than 10 people would be able to do it. If you want something harder, try this. @Advecticity too, if you're interested in the harder challenge.

  11. webspider
    Date: Fri, Jan 13 2012 09:29:01

    Mats wrote: I bet if I put a simple programming excersize on UPSB, less than 10 people would be able to do it.
    Maybe this has to do with the amount of people actually programming here :unsure:

  12. Mats
    Date: Fri, Jan 13 2012 09:35:28

    webspider wrote: Maybe this has to do with the amount of people actually programming here :unsure:
    Well the whole point was, in the shoutbox discussion, someone said it was a very popular hobby. I said it was perhaps popular to know how to do reeally basic stuff (like

    Hello

    for example) but not popular to actually be able to program even a simple task.

  13. Timbo
    Date: Fri, Jan 13 2012 12:14:42

    Spoiler #include #define MAX_INPUT 100 #define SWAP(x,y) (x^=y;y^=x;x^=y;) int isPrime( int num ) { int i; for( i = 2; i < num; i++ ) { if( num % i == 0 ) { return 0; } } return 1; } int main( void ) { unsigned int numlist[MAX_INPUT]; int i; int swaps; for( i = 0; i < MAX_INPUT; i++ ) { scanf( "%d", numlist + i ); } do { swaps = 0; for( i = 0; i < MAX_INPUT - 1; i++ ) { if( numlist[i] > numlist[i+1] ) { SWAP(numlist[i], numlist[i+1]); swaps += 1; } } }while( swaps ); for( i = 0; i < MAX_INPUT; i++ ) { if( isPrime( numlist[i] ) ) { printf( "\n%d is prime", numlist[i]); } } return 0; }
    No idea if this will work or not. This isn't usually how I write code. If you expect 10 active members to be able to write code then I don't know how lucky you'll be. Pen spinning is more popular with teens that adults and most that know how to program are those that did a CS course. I spend that long on the fucking indentation and the spoiler tags don't even preserve it.

  14. Mats
    Date: Fri, Jan 13 2012 12:23:55

    Timbo wrote: No idea if this will work or not. This isn't usually how I write code.
    Won't compile for me. 3 errors given all to do with your SWAP at line 40. I'm sure you could resolve that easily enough though. That makes 3 people so far?
    If you expect 10 active members to be able to write code then I don't know how lucky you'll be.
    Me niether. That's why this is here. ;)
    I spend that long on the fucking indentation and the spoiler tags don't even preserve it.
    That is why code tags exist. Check this out:
    Spoiler
    
    #include 
    
    #define MAX_INPUT 100
    #define SWAP(x,y) (x^=y;y^=x;x^=y;)
    
    int isPrime( int num )
    {
             int i;
    
             for( i = 2; i < num; i++ )
             {
                     if( num % i == 0 )
                     {
                             return 0;
                     }
             }
    
             return 1;
    }
    
    int main( void )
    {
            unsigned int numlist[MAX_INPUT];
            int i;
            int swaps;
            
            for( i = 0; i < MAX_INPUT; i++ )
            {
                    scanf( "%d", numlist + i );
            }
    
    
            do
            {
                    swaps = 0;
                    for( i = 0; i < MAX_INPUT - 1; i++ )
                    {
                             if( numlist[i] > numlist[i+1] )
                             {
                                     SWAP(numlist[i], numlist[i+1]);
                                     swaps += 1;
                             }
                    }
            }while( swaps );
    
            for( i = 0; i < MAX_INPUT; i++ )
            {
                    if( isPrime( numlist[i] ) )
                    {
                            printf( "\n%d is prime", numlist[i]);
                    }
            }
    
            return 0;
    }

  15. Timbo
    Date: Fri, Jan 13 2012 13:48:45

    Removing the terminating semi-colon on line 40 should resolve those errors. Didn't think this forum had code tags, learn something new everyday.

  16. Retro-spectre
    Date: Fri, Jan 13 2012 21:06:28

    [CODE]#include #include #include #include using namespace std; const bool isPrime(const unsigned int &num) { if (num < 2) return false; for (unsigned int k = 3; k <= sqrt(num); k+=2) { if ((num % k) == 0) return false; } return true; } int main(int argc, char** argv) { vector suckme; for (int k = 0; k < 100; ++k) { unsigned int j; cin>>j; suckme.push_back(j); } sort(suckme.begin(), suckme.end()); for (vector::iterator it=suckme.begin(); it!=suckme.end(); ++it) { cout<<*it<<" "<<(isPrime(*it)?"- Prime":"- Not Prime")<

  17. Advecticity
    Date: Fri, Jan 13 2012 21:51:28

    While I suppose that any answer that works is a good answer, I think, Mats, that you should add some rule that you can't use default functions in certain cases. I avoided using stuff like List.Sort() because I figured it wasn't really the point of the challenge.

  18. Retro-spectre
    Date: Fri, Jan 13 2012 22:02:56

    But the point of this problem was to define a prime generator. it doesn't take much thought to write a selection sort (with no previous knowledge of sorts). i'd imagine most people won't write a prime sieve in their coding career, but virtually everyone knows how to write basic sorts. the fun part is the generator anyway (if you've never done it before).

  19. Advecticity
    Date: Fri, Jan 13 2012 22:08:31

    I would consider sorting and checking for a small prime to be of equal difficulty, but I admit the latter is more interesting.

  20. GeeGeeGee
    Date: Sat, Jan 14 2012 00:57:57

    well i'm taking programming 1. i'm learning visual basic 2010. so I have NO clue what you guys talking about. nameLabel.Text = "I'm stupid." :D That's all I know.....

  21. Mats
    Date: Sat, Jan 14 2012 01:20:16

    Well, if anyone would like further challenges in a few days or so? Then I can create problems for which I believe there are no 'shortcuts', or at least, nothing to make the task significantly easier. It's very difficult to say you cannot default functions, because it's hard to explain exactly what is not allowed. That's 4 people with a solution? Nearly halfway. :P Edit: Retro - Works fine. :)