jeudi 3 août 2023

How to assign array values to the n-1 location in the array

I have an array like this: [1, 2, 3, 4, 5]. I shuffle it to get this: [1, 5, 4, 2, 3].

The Goal: Assign each value in the array to the n-1 location in the array

In other words, I need the number 1 to be in 1st position, 2 in 5th, 3 in 4th, 2 in 5th, and 3 in 6th.

For example, the output of this one would be: [1, 4, 5, 3, 2].

Is there a built in function for this or a simple code that can accomplish this goal?

What I've done so far:

import numpy as np
import random
array = np.arange(1,6)
print(array)
random.shuffle(array)
print(array)



mercredi 2 août 2023

Why do I get an error In Accessing File for Bash Code

I Wanted to Make A Game To Just Spend Some Time Improving My Programming Skills.. I Got An Error, I tried Every Thing To Try To Fix It, Is There Any Solution? This is The Code

random=$((1 + $RANDOM % 10))
read -p "Enter A UserName" name
if [ -d $name ]
then
    cd $name
    if [ -d Scores ]
    then
        cd Scores
        else
                mkdir Scores
                cd Scores
                cd ../../
    fi
else
    mkdir $name
    cd $name
    mkdir Scores
    cd Scores
    cd ../../
fi
echo 'enter a random number'
read -p "Enter" r
if [ $r = $random ]
then
        echo 'great' $name
else
        echo 'nope' $name 'the number was' $random
fi

echo 'want to play again(y for play again n for dont play again)'
read m
if [ $m = y ]
then
        chmod +x AnswerDebug.sh
    bash AnswerDebug.sh
elif [ $m = n ]
then
        echo 'end of game'
else
echo 'not an input' $name
fi

The Output Was : The Output

I was programming a game where we have to find a random number. I tried to make a Scoring System but it is unfinished

I expected it to work finely but for some reason the code isn't working The code can't access AnswerDebug.sh(The Name Of The File)




I have no idea how to start this homework assignment. Can someone help guide me in a general direction?

Assignment Here

Hello everyone, Im a first year ICS 111 and we are currently on the topic of For and While loops. I attended the class and even rewatched the hour long lecture about 3 times now and have no idea where to even begin trying to work on this. So far, I've only just initialized the random numbers and Im trying to figure out how to start on my loop. I think im supposed to use a while loop since it's not specified how many times i would have to run this. The other assignments are not even close to what this one was, they were mostly straight forward from the lesson. Any sense of direction would help, I am completely lost. Thank you.

So far ive tried using a random int from -100 to 100, seeing as how north is positive and south would minus, however, I realized that since they are walking, it would only increase or decrease the X or Y coordinate one at a time, thus making that useless as it created random numbers like 50 or -11.




How to save the last RANDOM generated number in a loop

**I am trying to make the user go back to the position he was on if he gets higher than 100. So if he was on 97 and rolls a 5 on Dice1 and becomes 102 I want the variable position1 to go back to 97 I am still new so it might just be a stupid logic problem also my variable names are not industry standard so im still working on this **

     Scanner Scan = new Scanner(System.in);
     Random random = new Random();
     int Position1 = 0;
     //int position2 = 0;
    
     
     System.out.println("Type Start");
     String Input = Scan.next();

  
      if (Input.equals("start")){
         
         while(Position1!=100 ) {
            
             int Dice1 = random.nextInt(1,7);
            
 
             if (Dice1 == 6) {
                 Position1= Position1+Dice1;
                 System.out.println(Position1);
                 System.out.println("player 1 turn ");
                 Input = Scan.next();

             }
             else if (Dice1 == 5) {
                 Position1= Position1+Dice1;
                 System.out.println(Position1); 
                 System.out.println("player l turn");
                 Input = Scan.next();

             }
             else if (Dice1 == 4) {
                 Position1= Position1+Dice1;
                 System.out.println(Position1);
                 System.out.println("player 1 turn ");
                 Input = Scan.next
             }
             else if (Dice1 == 5) {
                 Position1= Position1+Dice1;
                 System.out.println(Position1); 
                 System.out.println("player l turn");
                 Input = Scan.next();

             }
             else if (Dice1 == 4) {
                 Position1= Position1+Dice1;
                 System.out.println(Position1);
                 System.out.println("player 1 turn ");
                 Input = Scan.next();

             }
             else if (Dice1 == 3) {
                 Position1= Position1+Dice1;
                 System.out.println(Position1);
                 System.out.println("Player 1 turn");
                 Input = Scan.next();

             }
             else if (Dice1 == 2) {
                 Position1= Position1+Dice1;
                 System.out.println(Position1);
                 System.out.println("player 1 turn");
                 Input = Scan.next();

             }
             else if (Dice1 == 1) {
                 Position1= Position1+Dice1;
                 System.out.println(Position1);
                 System.out.println("player 1 turn ");
                 Input = Scan.next();
             }
             if(Position1>100) {
                Position1 = Position1-Dice1;
             }
             else if(Position1==100) {
                 System.out.println("U Win"); 
                 
             }
                    
        }
        
     
    }

}

}




mardi 1 août 2023

Create a funnel using random boolean

I would like to create a 4-column chart that represents a customer journey having the following roles:

  • at most left columns only numbers 1
  • at the second column 35% of the 1 is still 1 and the rest is 0
  • at the third column 15% of the 1 on the second column is still 1 and the rest is 0
  • at the fourth column 2.5% of the 1 on the third column is still 1 and the rest is 0

I have tried the function below but it didn't work because I get only 0 on the fourth column:

def create_user_journey_table():
return [[1, 0, 0, 0]]
current_page = user_journey[-1][:]
user_journey.append([1, next_page_2, next_page_3, next_page_4])

next_page_2 = 1 if random.random() < 0.35 else 0
next_page_3 = 1 if (next_page_2 == 1 and random.random() < 0.15) else 0
next_page_4 = 1 if (current_page[0] == 1 and random.random() < 0.028) else 0

user_journey = create_user_journey_table()

Does anyone have any clue?

Thank you a lot!




DB Index-friendly way of generating random codes?

I need to generate 8-digit codes serving as identifiers and store them in the (SQL) database. Now if I want to avoid straight sequence like 000001, 000002 etc. thus I need some random generator.

Yet by random value, to ensure uniqueness I need to compare new one with all already stored. This obviously will cost me more and more time as the number of codes growths.

Is there a way to have pseudo random patterns which will be index-friendly so they could be compared in limited time?




Get a sequence of samples (w/o replacement) in order given the range and number of samples in C++ [closed]

I want to sample without replacement a range but I don't want to materialize all the samples in memory. In other words I want to first get the minimum sample, given that and the number of remaining samples to get the next one etc.

Fundamentally I want a function with the following signature.

int next_sample(int prev_sample, int upper_bound, int remaining_samples);