* FAQ    * Search  * Register * Login 
Active topics
Unanswered topics

All times are UTC-06:00



Post new topic  Reply to topic  [ 6 posts ] 
Author Message
 Post subject: a quick challenge
PostPosted: Thu Dec 22, 2011 1:56 pm 
Offline
DBB DemiGod
DBB DemiGod
User avatar

Joined: Sat Oct 24, 1998 2:01 am
Posts: 6415
Location: Calgary Alberta Canada
A UPC is a 12-digit number.
Typically its starts as a eleven digit number and a check digit is computed and stuck at the end.

The procedure to compute the check digit for UPC thusly follows:
  1. sum the digits in the odd positions and multiply the result by 3
  2. add to that the even digits
  3. modulus the sum so far by 10
  4. if the result isn't 0, subtract from 10

the challenge is not to compute the check digit without using any string manipulation.


Top
   
 Post subject: Re: a quick challenge
PostPosted: Tue Dec 27, 2011 8:05 pm 
Offline
DBB Material Defender
DBB Material Defender
User avatar

Joined: Sun Nov 25, 2001 3:01 am
Posts: 4627
Location: Indiana, U.S.A.
I was doing good until you threw that double-negative at me in the last sentence. Screw this, this stuff is hard. ;P

fliptw wrote:
the challenge is not to compute the check digit without using any string manipulation.


BTW I've already not computed it without using string manipulation, I'm just keeping it to myself because it wouldn't be unfair to everyone else. :P


Top
   
 Post subject: Re: a quick challenge
PostPosted: Tue Dec 27, 2011 11:39 pm 
Offline
DBB DemiGod
DBB DemiGod
User avatar

Joined: Sat Oct 24, 1998 2:01 am
Posts: 6415
Location: Calgary Alberta Canada
ahh I was in a rush when I wrote it.

the challenge is to compute the check digit without using any string manipulation.


Top
   
 Post subject: Re: a quick challenge
PostPosted: Wed Dec 28, 2011 2:59 am 
Offline
DBB Master
DBB Master
User avatar

Joined: Sun Sep 05, 1999 2:01 am
Posts: 6377
Location: ☃☃☃
Code:
unsigned long long upc_check( unsigned long upc )
{
    unsigned long left = upc;
    int i, mod, sum = 0;
    for (i = 0; i < 11; ++i) {
        mod = left % 10;
        sum +=  mod + ((mod & ((i & 1) - 1)) << 1);
        left = left / 10;
    }
    return (unsigned long long) upc * 10 + (10 - (sum % 10)) % 10;
}


This C code should work as long as you have two's complement integers. It's a bit pedantic though, espcially if the C compiler partially or fully unrolls the loop.


Top
   
 Post subject: Re: a quick challenge
PostPosted: Sun Feb 05, 2012 8:39 pm 
Offline
DBB Artist
DBB Artist
User avatar

Joined: Mon Aug 01, 2005 8:47 am
Posts: 7124
Location: Ơ̸̦͇̲̬̭̱̰͎̞͈̣͎͚̳ͬ͋̃̀̇͊͂͋͐ͦ̽ͣ̂ͥ͊̅̀̚͠ B̶͖̯͉̜̰̲̓̔͋̈́ͅ È̯ Y̪̤̼͉̠̙͝
Darn it. I love string manipulation! I'll get back to this.

_________________
s☼-£♦и̫͍ͥ̍ͪ͌̓͗͡о̡̹̱͊̅ͮ̓̕͢б̧̝̻̪̤̳̜͐̓̉ͤ͢͜ ͙̬͙̆̑ͮ̐ͭ̾̂́͘i̎̌̾̓̽̀̈̓̀҉͉̙̦͎̘̝͕f̻͕͔̘ͣͣ̓͊̿͢͜ ͍͔͈͕̮̫ͣ̆ͮ̊͋/♂6Æ!♪╩"▲L└уͭ̂͐̇҉̴̣̼̞̠̯͓̺̞ф̜̊͌̈́̋̏̐́ц̨͔̮̿̇ ̨̛͖̙͖̖̮̗̱ͩ̆͞ͅа̥͇̞̖͚̟̅͐ͤ͞͠͠э̜̘̩̳̬͔̾ͯ̀ͫ̒̐̿ͅͅг̭̖̀ͦ̒̑ͥ̌ͮͫ͞ё͔̟̃ͬ̾̓͟ё̦̞̙̫͔̩͑̀͂ͯ̄̔̃̑̀͠ͅͅ


Top
   
 Post subject: Re: a quick challenge
PostPosted: Sun Feb 05, 2012 9:31 pm 
Offline
DBB Master
DBB Master
User avatar

Joined: Sun Sep 05, 1999 2:01 am
Posts: 6377
Location: ☃☃☃
There still has yet to be a good solution. :-P


Top
   
Display posts from previous:  Sort by  
Post new topic  Reply to topic  [ 6 posts ] 

All times are UTC-06:00


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  



Descent'rs have piloted these pages
 
The layout and contents contained within this site are © DescentBB.net 1997-2006.
Descent, Descent II are © Parallax Software Corporation.
Descent III is Outrage Entertainment.
Descent is a Trademark of Interplay Productions.

Miner Wars™ is trademark of Keen Software House s. r. o.
.


Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group