jeudi 28 mai 2020

if Statement to Override Value

I'm trying to implement this formula from a paper (ref):

enter image description here

I tried:

import statsmodels.api

n = 1 # samples
x = 1 # positive results

# Range of significance
Min=0
Max=1
Step=.01
Alpha = np.arange(Min,Max+Step,Step)

Low  = []
High = []

for  A in Alpha:
  low, high = statsmodels.stats.proportion.proportion_confint(x, n, alpha=A, method='jeffreys')
  if x == 0: low  == 0 # these lines
  if x == n: high == 1 # aren't working
  Low.append(low)
  High.append(high)

But that gives me this:

enter image description here

My if statements are supposed to override the values of low and high when x=0 or x=n. How can I get them to work please?

ref: Brown, L.D., Cai, T.T. and DasGupta, A., 2001. Interval estimation for a binomial proportion. Statistical science, pp.101-117.

Bash: rename duplicates for generated values in csv file

There is a bash script that create a email addresses in next format: first letter from name and full surname, lowercase +@example.com.

csv file:

id,location,name,email
1,1,John Smith,ab@dc.com
2,2,Paul Robinson,
3,3,Fidel Guererro,qw@er.com
4,4,John Smith,
...

Column Name can contain duplicates. In this case script should add 1 in email address (ex. for id=1 - jsmith@example.com, for id=4 - jsmith1@example.com). I try next script:

#!/bin/bash

while IFS=, read -r col1 col2 col3 col4
do
            if [ "$col1" == Id ]; then
                echo "${col1},${col2},${col3},${col4}"
                continue
            fi

    firstinitial=${col3:0:1}
    surname=$(echo $col3 | cut -d' ' -f2)

            if [[ $col4 ==  $col4 ]]; then
                col4=${firstinitial,}${surname,,}1@abc.com
                else
                col4=${firstinitial,}${surname,,}@abc.com
            fi

    echo "${col1},${col2},${col3},${col4}"
done < acc.csv

I recieve all addresses with 1. How can I change this script?

Operator precedence misunderstanding

Some of my knowledge about the topic is as following. If consecutive operators in an expression have the same precedence, a rule called associativity is used to decide the order in which those operators are evaluated. Further, left-associative operators of the same precedence are evaluated in order from left to right.

What I don't understand is why the following code doesn't throw exception.

if (

    object == null || 
    object.Flag && 
    object.Status == object2.Status

)

What if object is null? In this case, doesn't the call of object.Status throws exception because of high precedence of == over &&, likewise && over ||? I mean,

if (

    (object == null) || 
    (object.Flag && 
    (object.Status == object2.Status))

)

The call order,

  1. object.Status == object2.Status
  2. object.Flag
  3. object == null

What point do I overlook?

How to call function inside "if" statement?

I am trying to find the longest palindrome in a given string. I came across a strange behavior of if statement. posting both the codes here, in 1st code when I tried debugging everything is working fine till if statement, but even after getting True, it is not accessing the further code inside.

Whereas in the second code I stored the return value of palindrome function in the boolean var and then used it in if statement. now it is accessing the inside code.

I guess I am missing something very fundamental in python. Please can someone tell why is this happening?

Code 1:

def palindrom(temp):
    if(temp==temp[::-1]):
        return True
    else:
        return False

word=input("enter the string to check  ")
maxx=""                                    #to store the longest palindrome till now
l=len(word)
for i in range(0,l):
    # if(len(maxx)>l-i):                   ## to check if remaining string is smaller than maxx
    #     print(maxx)
    #     break
    temp=word[i]                           ## testing by adding one letter at a time
    for j in range(i+1,l):
        temp=temp+word[j]
        if(temp is palindrom(temp)  and  len(temp)>len(maxx) ):
            maxx=temp                      ##updating the maxx
print(maxx)

Code 2:

def palindrom(temp):
    if(temp==temp[::-1]):
        return True
    else:
        return False

word=input("enter the string to check  ")
maxx=""
l=len(word)
for i in range(0,l):
    # if(len(maxx)>l-i):  ## to check if remaining string is smaller than maxx
    #     print(maxx)
    #     break
    temp=word[i]
    for j in range(i+1,l):
        temp=temp+word[j]
        bul=palindrom(temp)  ##storting the return value of palindrome() in a boolean.
        if(bul and  len(temp)>len(maxx)):
            maxx=temp
print(maxx)

I want to prevent printing twice

Yesterday, I was helped by a grateful person to solve the problem, but it was blocked again. If I print it out like the picture below, the part error! ~ is output twice. If I change the order of what is written after if and what else is written, it is printed twice. How can I solve this?

enter image description here enter image description here

I got something wrong with the else statement [duplicate]

Note: I'm not a fluent english speaker, so you may see some grammar mistakes here. I'm kinda new to programming and i know i can fix it by putting another else/elif statement, i just trying to get the short possible lines to avoid spamming elif like yandev.

so the thing is, at the #functions i already put if below the gae variable's input, which says that if the input = y or Y then it runs the line below it, which is print("good. go to www.minecraft.net"). Now my problem is, when i try to input other letter than Y, it still runs the same line whilst there's already an else line underneath that i think should exclude other words than Y or y. For example, i already input the word "n" at the output console(?) but the output says "good. go to www.minecraft.net" instead the lines at the else statement. is there any possible solution without adding much more elif/else statement?

Click this link for the image to understand what am i talking about

javaScript, Rock, Scissor, Paper game error

I'm trying to build a game of Rock, Scissor, Paper. The code isn't working for some reason. Could anyone shed some light on please. Could mostly be some syntax errors. (I'm a beginner)

var pcChoices = ["r", "s", "p"];
var wins = 0;
var losses = 0;
var ties = 0;

document.onkeyup = function(event) {
  var userGuess = event.key;

  var pcGuess = pcChoices[Math.floor(Math.random() * pcChoices.length)];
  if ((userGuess === "r") || (userGuess === "s") || (userGuess === "p")) {
    if ((userGuess === "r") && (pcGuess === "s")) {
      wins++;
    } else if ((userGuess === "r") && (pcGuess === "p")) {
      losses++;
    } else if ((userGuess === "s") && (pcGuess === "r")) {
      losses++;
    } else if ((userGuess === "s") && (pcGuess === "p")) {
      wins++;
    } else if ((userGuess === "p") && (pcGuess === "r")) {
      wins++;
    } else if ((userGuess === "p") && (pcGuess === "s")) {
      losses++;
    } else if (userGuess === pcGuess) {
      ties++;
    }

    var html =
      "<p>You chose: " + userGuess + "</p>" +
      "<p>The pc chose: " + pcGuess + "</p>" +
      "<p>wins: " + wins + "</p>" +
      "<p>ties: " + ties + "</p>";

    document.querySelector("#game").innerHTML = html;

  }
};
<div id="game"></div>