LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-13-2015, 04:12 AM   #1
unclesamcrazy
Member
 
Registered: May 2013
Posts: 188

Rep: Reputation: 1
Facing problem while solving Josephus Problem in BASH


I want to solve this question using BASH

100 people standing in a circle in an order 1 to 100. No. 1 has a sword. He kills the next person (i.e. No. 2) and gives the sword to the next (i.e. No. 3). All people do the same until only 1 survives. Which number survives at the last?

If input is taken by keyboard, it should print the saved person number. Not only for hard coded 100 person.

Suppose I enter 5 as user input, it should print "3 is safe".
if I enter 11, it should print "7 is safe".
Please help me to write this script.

Thanks
 
Old 05-13-2015, 05:54 AM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,241

Rep: Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325
We're not going to write this for you, but we WILL help you write it.
Show us what you've written so far and what you think the problem is and we'll help you.
Have you read any good tutorials; there are plenty on the web.
You could look at this one http://rute.2038bug.com/index.html.gz
 
Old 05-14-2015, 10:35 AM   #3
unclesamcrazy
Member
 
Registered: May 2013
Posts: 188

Original Poster
Rep: Reputation: 1
I am using array but I am not getting correct results.
Code:
#!/bin/bash
arrayName=(1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20)
var=$(echo "${#arrayName[@]}")
i=1
while :
do
sleep 2
unset arrayName[$i]
i=`expr $i + 2`
if [ $i -gt $var ]
then
var=$(echo "${#arrayName[@]}")
i=1
fi
done
echo "${arrayName[@]}"
 
Old 05-14-2015, 08:28 PM   #4
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 840

Rep: Reputation: 380Reputation: 380Reputation: 380Reputation: 380
Hi, just a few minor suggestions:
1/ I would suggest that you use proper indentation to make the code more readable.
2/ When debugging shell scripts, it is a good idea to put set -xv at the begining of the script. That will allow you to follow each step the script does.
3/ When describing your problem, be more specific than "It doesn't work" or "I'm not getting correct results". What exactly does not work? What results are you getting and what results do you expect?
4/
Code:
arrayName=(1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20)
You can use brace expansion here:
Code:
arrayName=({1..20})
5/
Code:
var=$(echo "${#arrayName[@]}")
That's a very useless use of echo. just do
Code:
var="${#arrayName[@]}"
6/
Code:
while :
That's an infinite loop. Please make sure that the loop exits when its purpose is achieved.
7/
Code:
unset arrayName[$i]
I would recommend putting "arrayName[$i]" within double quotes to avoid pathname expansion. Also, keep in mind unsetting an array member will not reset numbering of the other elements.
8/
Code:
i=`expr $i + 2`
better written as
Code:
i=$(expr $i + 2)
or, even better
Code:
((i+=2))
9/
Code:
i=1
Don't forget array indices start at 0 in bash.
10/ I'll leave fixing the remaining flaws in the logic of the program as an exercise for the original poster.
 
1 members found this post helpful.
Old 05-15-2015, 02:53 AM   #5
unclesamcrazy
Member
 
Registered: May 2013
Posts: 188

Original Poster
Rep: Reputation: 1
Thanks for guidance, my mistake I did not paste the code in my question. First I wrote this code
Code:
#!/bin/bash
arrayName=({1..20})
var="${#arrayName[@]}"
echo $var
i=1
while [ $var -gt $i ]
do
unset arrayName[$i]
i=`expr $i + 2`
done
echo "${arrayName[@]}"
but it removes the alternate array values only once, I need to keep removing alternate values until only one remains.
I am facing problem in this part. I want to keep the loop alive but not for ever.
Please help
 
Old 05-15-2015, 03:35 AM   #6
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 840

Rep: Reputation: 380Reputation: 380Reputation: 380Reputation: 380
Quote:
Originally Posted by unclesamcrazy View Post
until only one remains.
So, how do you say the same thing using a math expression?

Last edited by millgates; 05-15-2015 at 04:00 AM.
 
Old 05-15-2015, 05:30 AM   #7
unclesamcrazy
Member
 
Registered: May 2013
Posts: 188

Original Poster
Rep: Reputation: 1
I would remove them alternatively until I get the answer.
Quote:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
1 3 5 7 9 11 13 15 17 19
1 5 9 13 17
1 9 17
9 17
9
but not able to find, how to do it using loop.
 
Old 05-15-2015, 07:02 AM   #8
rtmistler
Moderator
 
Registered: Mar 2011
Location: Sutton, MA. USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu
Posts: 4,110
Blog Entries: 10

Rep: Reputation: 1525Reputation: 1525Reputation: 1525Reputation: 1525Reputation: 1525Reputation: 1525Reputation: 1525Reputation: 1525Reputation: 1525Reputation: 1525Reputation: 1525
Quote:
Originally Posted by millgates View Post
So, how do you say the same thing using a math expression?
I agree with millgates. I'd solve this problem via algorithm/math first and then determine how best to code it. Something like:
Code:
// n[next] = n + 1 accounting for loop around
// n[next2] = n + 2 also accounting for loop around
while (n[next]) exists do
    if (n[next2]) exists then
        destroy (n[next])
        n = (n[next2])
    else
        destroy (n[next])
        // n stays the same
    endif
endwhile
Next thing the instructor may ask is to do this with something like 1347 entries and then maybe start heading for the concept of linked lists.

Last edited by rtmistler; 05-15-2015 at 07:04 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
[SOLVED] VICE problem solving kkoistinen Linux - Software 2 11-03-2013 09:48 AM
[SOLVED] can any one help me in solving my problem with c programme badriinvenkat Linux Mint 20 08-13-2010 05:28 AM
I have a problem solving addiction dohpaz General 8 05-25-2007 01:27 PM
Need help solving this internet problem Please banner Linux - General 7 06-28-2005 07:44 PM
Partition problem solving... true_atlantis Linux - General 5 10-30-2003 01:16 PM


All times are GMT -5. The time now is 08:57 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration