lundi 25 décembre 2017

While statement guessing game

Need help with this While statement. I can't for the life of me get line 19 to print. So my first question is how do I get line 19 to print. The second is, is the code skipping from line 19 to line 34 after too many wrong result. Is it possible to point me in the right direction without giving me the actual code. Thank You.

    import java.util.Scanner;

public class hiLow {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        String playAgain = "";
        do {
            //Create a random number for the user to guess
            int theNumber = (int)(Math.random() * 100 + 1);
            System.out.println( theNumber );
            int guess = 0;
            int numberOfTries = 0;
            while (guess != theNumber && numberOfTries != 7) {
                numberOfTries++;
                System.out.println("Guess a number between 1 and 100");
                guess = scan.nextInt();
                if ((int) numberOfTries > 7) {
                    System.out.print("You Lose");
                    System.out.println("Would you like to play again (y/n)?");
                    playAgain = scan.next();
                while (playAgain.equalsIgnoreCase("y"));
                    System.out.println("Thank you for playing!  Goodbye.");}

                else if (guess < theNumber) {
                    System.out.println("Your entered " + guess + " is too low.  Try again.  " + numberOfTries);}
                else if (guess > theNumber) {
                    System.out.println("Your entered " + guess + " is too high.  Try again.  " + numberOfTries);}
                else {
                    System.out.println(guess + " is correct.  You win!!!");}
            }//end of while loop for guessing
            System.out.println("Would you like to play again (y/n)?");
            playAgain = scan.next();
        }while (playAgain.equalsIgnoreCase("y"));
        System.out.println("Thank you for playing!  Goodbye.");
        scan.close();


    }

}

dimanche 24 décembre 2017

R : choosing from 1 to 15 countries and accumulate the value if it is same or over

I'd like to make codes to choose countries according to the number of 1 to 15 and accumulate the value if it is same or over.

Here is what I'm doing for the cases of three countries. But I'd like to make more general one, which doesn't have to make individual codes for each number of countries.

select3_sum <- 0
select3_subsum <- 0
select3_compare <- 0

for (j in 1:5025){

  select_index[j,18] <- 0 
  select3 <- 0 
  for(i in 1:13){
    select3_subsum <- 0 


    for(k in i+1:14){
      select3_compare <- select_index[j,i]
      select3_subsum <- select3_compare 

      for(a in k+1:15){
        denon <- 1 

       if(select3_compare <= select_index[a,l]){
         select3_compare <- select_index[a,l]
         denon <- denon+1
         select3_subsum <- select3_subsum+select3_compare
       }
        select3_sum <- select3_sum + (select3_subsum/denon)
      }

      if(select3_compare <= select_index[j,k]){
        select3_compare <- select_index[j,k]
        denon <- denon+1
        select3_subsum <- select3_subsum+select3_compare 
      }
      else{
        select3_subsum <- select3_subsum
      }

      select3_sum <- select3_sum + (select3_subsum/denon)
    }
  }
  select3 <-(select3_sum/choose(15,3)) 
  select_index[j,18]<- select3
  select3_sum <- 0 

data is like below

structure(list(V1 = structure(c(2L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("6", 
"CHILE.index"), class = "factor"), V2 = structure(c(2L, 1L, 1L, 
1L, 1L, 1L, 1L), .Label = c("6", "SINGAPORE.index"), class = "factor"), 
    V3 = structure(c(2L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("7", 
    "EFTA.index"), class = "factor"), V4 = structure(c(2L, 1L, 
    1L, 1L, 1L, 1L, 1L), .Label = c("6", "USA.index"), class = "factor"), 
    V5 = structure(c(2L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("7", 
    "ASEAN.index"), class = "factor"), V6 = structure(c(2L, 1L, 
    1L, 1L, 1L, 1L, 1L), .Label = c("7", "INDIA.index"), class = "factor"), 
    V7 = structure(c(2L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("7", 
    "EU.index"), class = "factor"), V8 = structure(c(2L, 1L, 
    1L, 1L, 1L, 1L, 1L), .Label = c("7", "PERU.index"), class = "factor"), 
    V9 = structure(c(2L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("7", 
    "TURKEY.index"), class = "factor"), V10 = structure(c(2L, 
    1L, 1L, 1L, 1L, 1L, 1L), .Label = c("7", "COLOMBIA.index"
    ), class = "factor"), V11 = structure(c(2L, 1L, 1L, 1L, 1L, 
    1L, 1L), .Label = c("7", "AUSTRAILIA.index"), class = "factor"), 
    V12 = structure(c(2L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("6", 
    "CANADA.index"), class = "factor"), V13 = structure(c(2L, 
    1L, 1L, 1L, 1L, 1L, 1L), .Label = c("7", "NEW ZEALAND.index"
    ), class = "factor"), V14 = structure(c(2L, 1L, 1L, 1L, 1L, 
    1L, 1L), .Label = c("7", "VIETNAM.index"), class = "factor"), 
    V15 = structure(c(2L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("7", 
    "CHINA.index"), class = "factor"), V16 = structure(c(2L, 
    1L, 1L, 1L, 1L, 1L, 1L), .Label = c("0", "select1"), class = "factor"), 
    V17 = structure(c(2L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("0", 
    "select2"), class = "factor"), V18 = structure(c(2L, 1L, 
    1L, 1L, 1L, 1L, 1L), .Label = c("0", "select3"), class = "factor"), 
    V19 = structure(c(2L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("0", 
    "select4"), class = "factor"), V20 = structure(c(2L, 1L, 
    1L, 1L, 1L, 1L, 1L), .Label = c("0", "select5"), class = "factor"), 
    V21 = structure(c(2L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("0", 
    "select6"), class = "factor"), V22 = structure(c(2L, 1L, 
    1L, 1L, 1L, 1L, 1L), .Label = c("0", "select7"), class = "factor"), 
    V23 = structure(c(2L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("0", 
    "selec8"), class = "factor"), V24 = structure(c(2L, 1L, 1L, 
    1L, 1L, 1L, 1L), .Label = c("0", "selec9"), class = "factor"), 
    V25 = structure(c(2L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("0", 
    "selec10"), class = "factor"), V26 = structure(c(2L, 1L, 
    1L, 1L, 1L, 1L, 1L), .Label = c("0", "selec11"), class = "factor"), 
    V27 = structure(c(2L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("0", 
    "selec12"), class = "factor"), V28 = structure(c(2L, 1L, 
    1L, 1L, 1L, 1L, 1L), .Label = c("0", "selec13"), class = "factor"), 
    V29 = structure(c(2L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("0", 
    "selec14"), class = "factor"), V30 = structure(c(2L, 1L, 
    1L, 1L, 1L, 1L, 1L), .Label = c("0", "selec15"), class = "factor")), .Names = c("V1", 
"V2", "V3", "V4", "V5", "V6", "V7", "V8", "V9", "V10", "V11", 
"V12", "V13", "V14", "V15", "V16", "V17", "V18", "V19", "V20", 
"V21", "V22", "V23", "V24", "V25", "V26", "V27", "V28", "V29", 
"V30"), class = "data.frame", row.names = c(NA, -7L))
I'd like to calculate the average of accumulation of index(accumulate in case it is same or over, if not then it is not included in calculation)

I've already got results for cases when the number of countries is one and two. the results is like below.

structure(list(X = 1:3, CHILE.index = c(6L, 6L, 6L), SINGAPORE.index = c(6L, 
6L, 6L), EFTA.index = c(7L, 7L, 7L), USA.index = c(6L, 6L, 6L
), ASEAN.index = c(7L, 7L, 7L), INDIA.index = c(7L, 7L, 7L), 
    EU.index = c(7L, 7L, 7L), PERU.index = c(7L, 7L, 7L), TURKEY.index = c(7L, 
    7L, 7L), COLOMBIA.index = c(7L, 7L, 7L), AUSTRAILIA.index = c(7L, 
    7L, 7L), CANADA.index = c(6L, 6L, 6L), NEW.ZEALAND.index = c(7L, 
    7L, 7L), VIETNAM.index = c(7L, 7L, 7L), CHINA.index = c(7L, 
    7L, 7L), select1 = c(6.733333333, 6.733333333, 6.733333333
    ), select2 = c(6.804603175, 6.804603175, 6.804603175), select3 = c(0L, 
    0L, 0L), select4 = c(0L, 0L, 0L), select5 = c(0L, 0L, 0L), 
    select6 = c(0L, 0L, 0L), select7 = c(0L, 0L, 0L), selec8 = c(0L, 
    0L, 0L), selec9 = c(0L, 0L, 0L), selec10 = c(0L, 0L, 0L), 
    selec11 = c(0L, 0L, 0L), selec12 = c(0L, 0L, 0L), selec13 = c(0L, 
    0L, 0L), selec14 = c(0L, 0L, 0L), selec15 = c(0L, 0L, 0L)), .Names = c("X", 
"CHILE.index", "SINGAPORE.index", "EFTA.index", "USA.index", 
"ASEAN.index", "INDIA.index", "EU.index", "PERU.index", "TURKEY.index", 
"COLOMBIA.index", "AUSTRAILIA.index", "CANADA.index", "NEW.ZEALAND.index", 
"VIETNAM.index", "CHINA.index", "select1", "select2", "select3", 
"select4", "select5", "select6", "select7", "selec8", "selec9", 
"selec10", "selec11", "selec12", "selec13", "selec14", "selec15"
), class = "data.frame", row.names = c(NA, -3L))

How can I make codes to calculate all cases(the number of countries from 1 to 15) without being too complex?

thank you for all.

Bash, syntax error: 'elif' unexpected after elif line

I googling about an hour to find why my bash script is error and i feel my brain is ready to explode cause no answers, please help me

dtc=$(df /cache | awk '{print $4}' | awk 'NR==2')
    if [ "$dtc" -gt "196608" ]; then
        echo -ne "|         |  0%|         |\r"
        su -c "/system/0211/installer/swap/0211swaptocache192mb.sh"
        sleep 0.8
        echo -ne "|>>>>>>   | 30%|         |\r"
        mkswap /cache/0211swap.img
        swapon -p 1 /cache/0211swap.img
        echo 100 >  /proc/sys/vm/swappiness
        sleep 0.8
        echo -ne "|>>>>>>>>>| 55%|>        |\r"
        cp /system/0211/installer/swap/0211swapon /system/etc/init.d/0211swapon
        sleep 0.8
        echo -ne "|>>>>>>>>>|100%|>>>>>>>>>|\r"
        chmod 777 /system/etc/init.d/0211swapon
        sleep 1
        echo -ne "|>>>>>>>>>|Done|>>>>>>>>>|\n"
        sleep 0.8
        echo "If nothing wrong, your Swap is 192MB and should active"
        sleep 0.8
        else
        echo "/cache partition: not enough space"
        sleep 0.8
    elif [ "$dtc" -gt "163840" ]; then
        echo -ne "|         |  0%|         |\r" #this is the error
        su -c "/system/0211/installer/swap/0211swaptocache160mb.sh"
        sleep 0.8
        echo -ne "|>>>>>>   | 30%|         |\r"
        mkswap /cache/0211swap.img
        swapon -p 1 /cache/0211swap.img
        echo 100 >  /proc/sys/vm/swappiness
        sleep 0.8
        echo -ne "|>>>>>>>>>| 55%|>        |\r"
        cp /system/0211/installer/swap/0211swapon /system/etc/init.d/0211swapon
        sleep 0.8
        echo -ne "|>>>>>>>>>|100%|>>>>>>>>>|\r"
        chmod 777 /system/etc/init.d/0211swapon
        sleep 1
        echo -ne "|>>>>>>>>>|Done|>>>>>>>>>|\n"
        sleep 0.8
        echo "If nothing wrong, your Swap is 160MB and should active"
        sleep 0.8
    fi

Can someone please help me? 'elif' unexpected but the error is at the line after the elif

R : making if and for statements simpler

select3_sum <- 0
select3_subsum <- 0
select3_compare <- 0

for (j in 1:5025){

  select_index[j,18] <- 0 
  select3 <- 0 
  for(i in 1:13){
    select3_subsum <- 0 


    for(k in i+1:14){
      select3_compare <- select_index[j,i]
      select3_subsum <- select3_compare 

      for(a in k+1:15){
        denon <- 1 

       if(select3_compare <= select_index[a,l]){
         select3_compare <- select_index[a,l]
         denon <- denon+1
         select3_subsum <- select3_subsum+select3_compare
       }
        select3_sum <- select3_sum + (select3_subsum/denon)
      }

      if(select3_compare <= select_index[j,k]){
        select3_compare <- select_index[j,k]
        denon <- denon+1
        select3_subsum <- select3_subsum+select3_compare 
      }
      else{
        select3_subsum <- select3_subsum
      }

      select3_sum <- select3_sum + (select3_subsum/denon)
    }
  }
  select3 <-(select3_sum/choose(15,3)) 
  select_index[j,18]<- select3
  select3_sum <- 0 

Here is what I gave up for cases of three countries.

data is like below

structure(list(V1 = structure(c(2L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("6", 
"CHILE.index"), class = "factor"), V2 = structure(c(2L, 1L, 1L, 
1L, 1L, 1L, 1L), .Label = c("6", "SINGAPORE.index"), class = "factor"), 
    V3 = structure(c(2L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("7", 
    "EFTA.index"), class = "factor"), V4 = structure(c(2L, 1L, 
    1L, 1L, 1L, 1L, 1L), .Label = c("6", "USA.index"), class = "factor"), 
    V5 = structure(c(2L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("7", 
    "ASEAN.index"), class = "factor"), V6 = structure(c(2L, 1L, 
    1L, 1L, 1L, 1L, 1L), .Label = c("7", "INDIA.index"), class = "factor"), 
    V7 = structure(c(2L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("7", 
    "EU.index"), class = "factor"), V8 = structure(c(2L, 1L, 
    1L, 1L, 1L, 1L, 1L), .Label = c("7", "PERU.index"), class = "factor"), 
    V9 = structure(c(2L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("7", 
    "TURKEY.index"), class = "factor"), V10 = structure(c(2L, 
    1L, 1L, 1L, 1L, 1L, 1L), .Label = c("7", "COLOMBIA.index"
    ), class = "factor"), V11 = structure(c(2L, 1L, 1L, 1L, 1L, 
    1L, 1L), .Label = c("7", "AUSTRAILIA.index"), class = "factor"), 
    V12 = structure(c(2L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("6", 
    "CANADA.index"), class = "factor"), V13 = structure(c(2L, 
    1L, 1L, 1L, 1L, 1L, 1L), .Label = c("7", "NEW ZEALAND.index"
    ), class = "factor"), V14 = structure(c(2L, 1L, 1L, 1L, 1L, 
    1L, 1L), .Label = c("7", "VIETNAM.index"), class = "factor"), 
    V15 = structure(c(2L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("7", 
    "CHINA.index"), class = "factor"), V16 = structure(c(2L, 
    1L, 1L, 1L, 1L, 1L, 1L), .Label = c("0", "select1"), class = "factor"), 
    V17 = structure(c(2L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("0", 
    "select2"), class = "factor"), V18 = structure(c(2L, 1L, 
    1L, 1L, 1L, 1L, 1L), .Label = c("0", "select3"), class = "factor"), 
    V19 = structure(c(2L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("0", 
    "select4"), class = "factor"), V20 = structure(c(2L, 1L, 
    1L, 1L, 1L, 1L, 1L), .Label = c("0", "select5"), class = "factor"), 
    V21 = structure(c(2L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("0", 
    "select6"), class = "factor"), V22 = structure(c(2L, 1L, 
    1L, 1L, 1L, 1L, 1L), .Label = c("0", "select7"), class = "factor"), 
    V23 = structure(c(2L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("0", 
    "selec8"), class = "factor"), V24 = structure(c(2L, 1L, 1L, 
    1L, 1L, 1L, 1L), .Label = c("0", "selec9"), class = "factor"), 
    V25 = structure(c(2L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("0", 
    "selec10"), class = "factor"), V26 = structure(c(2L, 1L, 
    1L, 1L, 1L, 1L, 1L), .Label = c("0", "selec11"), class = "factor"), 
    V27 = structure(c(2L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("0", 
    "selec12"), class = "factor"), V28 = structure(c(2L, 1L, 
    1L, 1L, 1L, 1L, 1L), .Label = c("0", "selec13"), class = "factor"), 
    V29 = structure(c(2L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("0", 
    "selec14"), class = "factor"), V30 = structure(c(2L, 1L, 
    1L, 1L, 1L, 1L, 1L), .Label = c("0", "selec15"), class = "factor")), .Names = c("V1", 
"V2", "V3", "V4", "V5", "V6", "V7", "V8", "V9", "V10", "V11", 
"V12", "V13", "V14", "V15", "V16", "V17", "V18", "V19", "V20", 
"V21", "V22", "V23", "V24", "V25", "V26", "V27", "V28", "V29", 
"V30"), class = "data.frame", row.names = c(NA, -7L))

I'd like to calculate the average of accumulation of index(accumulate in case it is same or over, if not then it is not included in calculation)

I've already got results for cases when the number of countries is one and two. the results is like below.

structure(list(X = 1:3, CHILE.index = c(6L, 6L, 6L), SINGAPORE.index = c(6L, 
6L, 6L), EFTA.index = c(7L, 7L, 7L), USA.index = c(6L, 6L, 6L
), ASEAN.index = c(7L, 7L, 7L), INDIA.index = c(7L, 7L, 7L), 
    EU.index = c(7L, 7L, 7L), PERU.index = c(7L, 7L, 7L), TURKEY.index = c(7L, 
    7L, 7L), COLOMBIA.index = c(7L, 7L, 7L), AUSTRAILIA.index = c(7L, 
    7L, 7L), CANADA.index = c(6L, 6L, 6L), NEW.ZEALAND.index = c(7L, 
    7L, 7L), VIETNAM.index = c(7L, 7L, 7L), CHINA.index = c(7L, 
    7L, 7L), select1 = c(6.733333333, 6.733333333, 6.733333333
    ), select2 = c(6.804603175, 6.804603175, 6.804603175), select3 = c(0L, 
    0L, 0L), select4 = c(0L, 0L, 0L), select5 = c(0L, 0L, 0L), 
    select6 = c(0L, 0L, 0L), select7 = c(0L, 0L, 0L), selec8 = c(0L, 
    0L, 0L), selec9 = c(0L, 0L, 0L), selec10 = c(0L, 0L, 0L), 
    selec11 = c(0L, 0L, 0L), selec12 = c(0L, 0L, 0L), selec13 = c(0L, 
    0L, 0L), selec14 = c(0L, 0L, 0L), selec15 = c(0L, 0L, 0L)), .Names = c("X", 
"CHILE.index", "SINGAPORE.index", "EFTA.index", "USA.index", 
"ASEAN.index", "INDIA.index", "EU.index", "PERU.index", "TURKEY.index", 
"COLOMBIA.index", "AUSTRAILIA.index", "CANADA.index", "NEW.ZEALAND.index", 
"VIETNAM.index", "CHINA.index", "select1", "select2", "select3", 
"select4", "select5", "select6", "select7", "selec8", "selec9", 
"selec10", "selec11", "selec12", "selec13", "selec14", "selec15"
), class = "data.frame", row.names = c(NA, -3L))

How can I make codes to calculate all cases(the number of countries from 1 to 15) without being too complex?

In Coq, "if then else" allows non-boolean first argument?

I read in a few tutorials that if a then b else c stands for match a with true => b | false => c end. However the former very strangely does not check the type of a, while the latter of course makes sure that a is a boolean. For instance,

Coq < Check if nil then 1 else 2.
if nil then 1 else 2
     : nat
where
?A : [ |- Type] 


Coq < Check match nil with true => 1 | false => 2 end.
Toplevel input, characters 33-38:
> Check match nil with true => 1 | false => 2 end.
>                                  ^^^^^
Error: Found a constructor of inductive type bool while
a constructor of list is expected.

Why is if ... then ... else ... allowing its first argument to be anything else than a non-boolean? Is there some overloading going on? (Locate "if". gives no result.)

returns true for ((unsigned int)0-1)>0

I came across some c++ code which was like

if(((unsigned int)0-1)>0)
{
//do something
}

and the program executed the statements in the if block. Being curious, I tried the same in c, which did the same. My understanding is that the statements in the if block get executed if the expression in the if condition returns a bool value true. This means that ((unsigned int)0-1)>0 must be returning true. Why is this happening?

How can I double a character in a string when its a small letter (a-z) and triple it when its a big letter (A-Z)?

I want to to it like I described above. For example: input: abc output: aabbcc input: ABC output: AAABBBCCC

Thanks