LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-28-2012, 01:35 PM   #1
Razvanq
LQ Newbie
 
Registered: Jul 2012
Posts: 6

Rep: Reputation: Disabled
How to remove a certain digit from a number multiple times.Algorithm


Hi. I would like some help with this algorithm. So i have been trying for about one hour now to solve this simple algorithm (or at least that is what i thought when i first looked into it).

I need to remove a certain digit from a number which repeats itself multiple times.
As an example if our number is 52753 and we need to remove the digit 5 (what number needs to be deleted is typed from the keyboard). It should pop on the screen this number 273.

I have been trying to get the number in a vector and try to work it out from there but i didn't get anywhere this way. Also been trying to play around with / and % but i still didn't solve it.
I am pretty sure the algorithm isn't as complicated as i have thought but i just don't seem to be able to get on the good path to solve it.

Help would be greatly appreciated. Thank you in advance
 
Old 08-28-2012, 01:51 PM   #2
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by Razvanq View Post
As an example if our number is 52753 and we need to remove the digit 5 (what number needs to be deleted is typed from the keyboard). It should pop on the screen this number 273.
sed could be the "heart" of your solution.
Code:
echo "52753" | sed 's/5//g'
This takes the input (which could be a whole file of data) and replaces every 5 with the null string.
Code:
sed 's/5//g' < $InFile > $OutFile

Daniel B. Martin
 
Old 08-28-2012, 03:48 PM   #3
Razvanq
LQ Newbie
 
Registered: Jul 2012
Posts: 6

Original Poster
Rep: Reputation: Disabled
Thanks for the replay !
Well the commands that you posted aren't something that i have learned so i was wondering if there is a way to do this using commands like / or % in a while(). Or something similar.
 
Old 08-28-2012, 04:32 PM   #4
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
What language are you trying to do this in?

In bash:
Code:
$ var=52753
$ echo ${var//5/}
273
 
Old 08-28-2012, 06:06 PM   #5
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,187

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
Assuming you're using bash, and the string is in a variable called string, then short=${string//5} should create a short variable without any fives in it.

That doesn't address the question "How to identify the repeated digits?," but your "vector" method seem a reasonable approach.

If you don't need to use bash, here's a gawk program you could use
Code:
#!/bin/gawk -f
{
  delete count
  n=split($0,digit,"")
  for (i=1;i<=n;++i) {
    count[digit[i]]++
  }
  j=0
  for (i=1;i<=n;++i) {
    if (count[digit[i]]==1) {
      ++j
      printf("%d", digit[i])
    }
  }
  if (j) print " <- " $0
}
(If you don't want the source number, replace the if (j) print " <- " $0 with a if (j) print ""; else print)

With that change, I get the following with ten random five-digit numbers:
Code:
$ cat r10
23722
69450
92719
67290
42707
88180
13841
44521
06246
04079
$ gawk -f remove_dups.gawk r10
37
69450
271
67290
420
10
384
521
024
479
(You can make the gawk program executable by a chmod +x remove_dups.gawk where, of course, the remove_dups.gawk is whatever you named your copy of the program.)

Note: That program will remove any duplicated character(s) in the input file, not just digits.

<edit>
By the way, the probability that a random five digit number will have a all digits different is about 1/3 (It's exactly 0.9*0.8*0.7*0.6.), so the two I got out of ten is about right.)
</edit>

Last edited by PTrenholme; 08-28-2012 at 06:21 PM.
 
Old 08-29-2012, 03:34 PM   #6
Razvanq
LQ Newbie
 
Registered: Jul 2012
Posts: 6

Original Poster
Rep: Reputation: Disabled
I am writing in c++. Using Mingw developer studio to write in.
I think i understand. Thanks for help
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Regular expression to Grep for a n digit number somupl86 Linux - General 7 11-24-2010 06:11 AM
Substitue single-digit, two-digit, and 3-digit numbers with text using sed dmason165 Programming 13 08-07-2009 10:38 AM
15 digit long number Zagi Programming 3 07-14-2008 06:26 AM
how do i produce a single digit random number in BASH? jaepi Linux - Newbie 8 04-29-2007 11:54 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:03 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration