Next Challenge!

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Next Challenge!

Post by avansc »

okay, here is a nice simple one.

remember when you did old addition
as in.

226
+74
----
300

well if you notice there were 2 carries.
so lets say for every carry, you assign a difficulty point.

your job is to take 2 numbers and tell me how many carries occured.

another example would be.

59216
+43115
---------
102331

there were 3 carries this time.

a full licenced version of VS2008 will be up for offer. the entry that solves the problem with the most innovative way wins. if there are 2 alike, i will hold a poll that will decide who wins.

edit : this progam can be done in 10 lines of code easily. probably less.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: Next Challenge!

Post by avansc »

here is a hint. RECURSION
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: Next Challenge!

Post by avansc »

jeez, i must be more nerdy than i thought.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
M_D_K
Chaos Rift Demigod
Chaos Rift Demigod
Posts: 1087
Joined: Tue Oct 28, 2008 10:33 am
Favorite Gaming Platforms: PC
Programming Language of Choice: C/++
Location: UK

Re: Next Challenge!

Post by M_D_K »

I'm working on it. It's just hard to think right now :)
Gyro Sheen wrote:you pour their inventory onto my life
IRC wrote: <sparda> The routine had a stack overflow, sorry.
<sparda> Apparently the stack was full of shit.
User avatar
Kleithap
ES Web Developer
ES Web Developer
Posts: 324
Joined: Sat Jan 26, 2008 9:03 am
Location: Amsterdam

Re: Next Challenge!

Post by Kleithap »

avansc wrote:jeez, i must be more nerdy than i thought.
Heh, don't worry about it.

Here's a simple Java class that solves the problem:

Code: Select all

public class Addition {
	public static void main(String[] args) {
		java.util.Scanner input = new java.util.Scanner(System.in);
		System.out.print("Enter first number: ");
		int p = input.nextInt();
		System.out.print("Enter second number: ");
		int q = input.nextInt();
		System.out.println("Carries of " + p + " + " + q +
				" = " + numberCarries(p, q, 0));
	}

	public static int numberCarries(int p, int q, int n) {
		if ((p + q) < 10) {
			return n;
		} else {
			if ( ((p % 10) + (q % 10)) < 10)
				return numberCarries(p / 10, q / 10, n);
			else
				return numberCarries((11 + p) / 10, q / 10, n + 1);
		}
	}
}
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: Next Challenge!

Post by avansc »

i'll post some hints. some things i used.
i used mod, with base 10. "num%10= remainer" - this is how you isolate the last digit in the number like 1234%10=4
i have 2 if statements.
and i use recursion.

armed with that you should probably come to the same function as me. its only about 6-7 lines in length
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
Kleithap
ES Web Developer
ES Web Developer
Posts: 324
Joined: Sat Jan 26, 2008 9:03 am
Location: Amsterdam

Re: Next Challenge!

Post by Kleithap »

avansc wrote:i'll post some hints. some things i used.
i used mod, with base 10. "num%10= remainer" - this is how you isolate the last digit in the number like 1234%10=4
i have 2 if statements.
and i use recursion.

armed with that you should probably come to the same function as me. its only about 6-7 lines in length
Well, yeah that's exactly what I did. If you want to make the function shorter you can always do this:

Code: Select all

public class Addition {
	public static void main(String[] args) {
		java.util.Scanner input = new java.util.Scanner(System.in);
		System.out.print("Enter first number: ");
		int p = input.nextInt();
		System.out.print("Enter second number: ");
		int q = input.nextInt();
		System.out.println("Carries of " + p + " + " + q +
				" = " + numberCarries(p, q, 0));
	}

	public static int numberCarries(int p, int q, int n) {
		if ((p + q) < 10)
			return n;
		else
			return ( ((p % 10) + (q % 10)) < 10) ? numberCarries(p / 10, q / 10, n) : numberCarries((11 + p) / 10, q / 10, n + 1);
	}
}
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: Next Challenge!

Post by avansc »

Kleithap wrote:
avansc wrote:jeez, i must be more nerdy than i thought.
Heh, don't worry about it.

Here's a simple Java class that solves the problem:

Code: Select all

public class Addition {
	public static void main(String[] args) {
		java.util.Scanner input = new java.util.Scanner(System.in);
		System.out.print("Enter first number: ");
		int p = input.nextInt();
		System.out.print("Enter second number: ");
		int q = input.nextInt();
		System.out.println("Carries of " + p + " + " + q +
				" = " + numberCarries(p, q, 0));
	}

	public static int numberCarries(int p, int q, int n) {
		if ((p + q) < 10) {
			return n;
		} else {
			if ( ((p % 10) + (q % 10)) < 10)
				return numberCarries(p / 10, q / 10, n);
			else
				return numberCarries((11 + p) / 10, q / 10, n + 1);
		}
	}
}
here is a common mistake in recursion.

Code: Select all

public static int numberCarries(int p, int q) {
      if ((p + q) < 10) {
         return 0;
      } else {
         if ( ((p % 10) + (q % 10)) < 10)
            return 0 + numberCarries(p / 10, q / 10, n);
         else
            return 1 + numberCarries((11 + p) / 10, q / 10);
      }
   }
pretty much the same solution i came up with, but did you test it? i think there are values that it wont work for.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: Next Challenge!

Post by avansc »

Kleithap wrote:
avansc wrote:i'll post some hints. some things i used.
i used mod, with base 10. "num%10= remainer" - this is how you isolate the last digit in the number like 1234%10=4
i have 2 if statements.
and i use recursion.

armed with that you should probably come to the same function as me. its only about 6-7 lines in length
Well, yeah that's exactly what I did. If you want to make the function shorter you can always do this:

Code: Select all

public class Addition {
	public static void main(String[] args) {
		java.util.Scanner input = new java.util.Scanner(System.in);
		System.out.print("Enter first number: ");
		int p = input.nextInt();
		System.out.print("Enter second number: ");
		int q = input.nextInt();
		System.out.println("Carries of " + p + " + " + q +
				" = " + numberCarries(p, q, 0));
	}

	public static int numberCarries(int p, int q, int n) {
		if ((p + q) < 10)
			return n;
		else
			return ( ((p % 10) + (q % 10)) < 10) ? numberCarries(p / 10, q / 10, n) : numberCarries((11 + p) / 10, q / 10, n + 1);
	}
}
your code does not work, it produces 2 carries for the numbers 1901 and 99, it should be 3.

this little challange is trickier than you might think. and illustrates why you should allways test your code.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
Kleithap
ES Web Developer
ES Web Developer
Posts: 324
Joined: Sat Jan 26, 2008 9:03 am
Location: Amsterdam

Re: Next Challenge!

Post by Kleithap »

avansc wrote:your code does not work, it produces 2 carries for the numbers 1901 and 99, it should be 3.
Oops, you're right.

I've quickly made a simple one that uses an actual carry flag :D

Code: Select all

public static int numberCarries(int p, int q, int carry) {
      if ((p + q) + carry < 10)
         return 0;
      else
         return ( ((p % 10) + (q % 10) + carry) < 10) ? 0 + numberCarries(p / 10, q / 10, 0) : 1 + numberCarries(p / 10, q / 10, 1);
 }
It can probably be made more efficient but I don't have any time right now and I couldn't leave you without a solution :) . I didn't really have time to test this one, if you find any other bugs be sure to let me know. If I have time I'll look for a better solution tomorrow.
avansc wrote:this little challange is trickier than you might think. and illustrates why you should allways test your code.
I'm always happy when my sloppiness can be of use to other people.
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: Next Challenge!

Post by avansc »

Kleithap wrote:
avansc wrote:your code does not work, it produces 2 carries for the numbers 1901 and 99, it should be 3.
Oops, you're right.

I've quickly made a simple one that uses an actual carry flag :D

Code: Select all

public static int numberCarries(int p, int q, int carry) {
      if ((p + q) + carry < 10)
         return 0;
      else
         return ( ((p % 10) + (q % 10) + carry) < 10) ? 0 + numberCarries(p / 10, q / 10, 0) : 1 + numberCarries(p / 10, q / 10, 1);
 }
It can probably be made more efficient but I don't have any time right now and I couldn't leave you without a solution :) . I didn't really have time to test this one, if you find any other bugs be sure to let me know. If I have time I'll look for a better solution tomorrow.
avansc wrote:this little challange is trickier than you might think. and illustrates why you should allways test your code.
I'm always happy when my sloppiness can be of use to other people.
hahah, thats almost verbatim what i had. nice work. unless someone gets something better you have yourself a copy of VS2008
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
Kleithap
ES Web Developer
ES Web Developer
Posts: 324
Joined: Sat Jan 26, 2008 9:03 am
Location: Amsterdam

Re: Next Challenge!

Post by Kleithap »

avansc wrote:hahah, thats almost verbatim what i had.
Haha, great minds think alike :P

Seriously though, let everybody know when you think of another challenge, this one was pretty cool!
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: Next Challenge!

Post by avansc »

Kleithap wrote:
avansc wrote:hahah, thats almost verbatim what i had.
Haha, great minds think alike :P

Seriously though, let everybody know when you think of another challenge, this one was pretty cool!
or is it "fools seldom differ"
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
Kleithap
ES Web Developer
ES Web Developer
Posts: 324
Joined: Sat Jan 26, 2008 9:03 am
Location: Amsterdam

Re: Next Challenge!

Post by Kleithap »

Well, yeah, that's probably more accurate.
User avatar
Kleithap
ES Web Developer
ES Web Developer
Posts: 324
Joined: Sat Jan 26, 2008 9:03 am
Location: Amsterdam

Re: Next Challenge!

Post by Kleithap »

avansc wrote:hahah, thats almost verbatim what i had. nice work. unless someone gets something better you have yourself a copy of VS2008
Are you serious about VS2008? I mean it's really cool, but I don't live in the US so the costs sending me a copy are probably relatively high.
Post Reply