jeudi 27 juillet 2017

Python Check if list item does (not) contain any of other list items

I have this problem where I want to remove a list element if it contains 'illegal' characters. The legal characters are specified in multiple lists. They are formed like this, where alpha stands for the alphabet (a-z + A-Z), digit stands for digits (0-9) and punct stands for punctuation (sort of).

alpha = list(string.ascii_letters)
digit = list(string.digits)
punct = list(string.punctuation)

This way I can specify something as an illegal character if it doesn't appear in one of these lists.

After that I have a list containing elements:

Input = ["Amuu2", "Q1BFt", "dUM€n", "o°8o1G", "mgF)`", "ZR°p", "Y9^^M", "W0PD7"]

I want to filter out the elements containing illegal characters. So this is the result I want to get (doesn't need to be ordered):

var = ["Amuu2", "Q1BFt", "mgF)`", "Y9^^M", "W0PD7"]

I don't really have an idea on how to achieve this, and I have already tried various things.

What is a reasonable String equals check in rust?

I'd like to compare 2 strings in Rust. I'm pretty sure that both "strings" are already strings!

But no matter what the user enters, it always comes the Blacklistmode!

Here is what i've tried:

//Imports
use std::io;

fn main() {
    //Greeting
    println!("[INFO] Welcome to Tracer!\n");
    println!("[ ?? ] Use Blacklist mode ? [Y/n]");

    //Get user input
    let users_choice = get_input();

    //Check if user wants to run in Blacklist Mode
    if users_choice == ToString::to_string("n") || users_choice == ToString::to_string("N") {
        //User wants to use Whitelist mode
        trace_network(0);
    } else {
        //User wants to use Blacklist mode
        trace_network(1);
    }
}

fn get_input() -> String {

    //Creating changeable var
    let mut user_input = String::new();

    //Read Input; On Error drop MSG
    io::stdin().read_line(&mut user_input)
        .expect("[FAIL] Failed to read line");

    println!("[INFO] Your input was: >{}<", user_input);

    return user_input;
}

fn trace_network(mode:i32) {
    println!("[INFO] Starting trace !!! {}", mode);

    //Print wich mode was choosen
    if mode == 0 {
        println!("[INFO] Whitelistmode!");
    } else {
        println!("[INFO] Blacklistmode!");
    }
}

Improve my coding "for loop"

The following is a simple loop to insert a new column in a data frame after checking a specific condition (if 2 consecutive rows have the same value). The code works just fine but I would like to improve my coding skills so I ask for alternative solutions (faster, more elegant). I checked previous threads on the topic and learned a lot but I am curious about my specific case. Thanks for any input.

vector<-1
vector_tot<-NULL

  for(i in 1:length(dat$Label1))
  { 
     vector_tot<-c(vector_tot,vector)
     if(dat$Label1[i]==dat$Label1[i+1]){
    vector<-0  
    }
    else {
      vector<-1
      }
      }


dat$vector<- vector_tot

IF statement returning false with no data

Forgive me for my trivial question, I'm sure the answer is simple, but I've been looking at spreadsheets and VBA for the whole week and my brain is fried.

I am working on an elongated IF statement that produces different outputs depending on the start of a part number e.g. 3XXXXXXX = Software, 4XXXXXXX = Hardware.

The problem I am having is that the formula outputs 'FALSE' when a part number is yet to be given to the row.

=IF(B2>0,IF(LEFT(D2,1)="1","Assembly",IF(LEFT(D2,1)="2","Sub-Assembly",IF(LEFT(D2,1)="3","Software",IF(LEFT(D2,1)="4","Hardware",IF(LEFT(D2,1)="5","Chemical",IF(LEFT(D2,1)="6","Spare",IF(LEFT(D2,1)="7","Spare",IF(LEFT(D2,1)="8","Document",IF(LEFT(D2,1)="9","Misc",""))))))))))

Please let me know if you can spot where I am going wrong!

Thanks, Dan

Compare image in picture-box to default image

I assigned Picturebox.Image to Default image. But It still pass the If-statement and open new Diaglog. I don't know why? Please help me!

private void saveBtn_Click(object sender, EventArgs e)
{
    if (imgBox.Image == Properties.Resources.DefaultImage || imgBox.Image == Properties.Resources.EmptyPhoto)
    {
        MessageBox.Show("Warning! Cannot save this image!");
    }
    else
    {
        SaveNoti save = new SaveNoti();
        save.sender = new SaveNoti.SEND(Task_functions);
        save.ShowDialog();
    }
}

VBA - IF evaluation

I am pretty sure that in most languages you can secure the evaluation of a condition by doing one of the following:

if FALSE and (1/0 = 0) {...}

or

if TRUE or (1/0 = 0) {...}

Those examples should not throw exeptions (maybe the second one depending on internal implementation of the evaluation) as the whole if-condition is met even without checking the second condition.

In VBA however both these protections wont work:

If False and t() Then ...
If True or t() Then ...

Function t() As Boolean
MsgBox "This is displayed both times"
End Function

Why does VBA behave this way and what is the best (+shortest) way to do these kind of protections instead?

Example: If s = "" or thisFunctionCantTakeEmptyStrings(s) Then ...

Bash: shell script if statement using multiple conditions including regex

I am currently studying the shell script and having some syntax issue.

what I am tyring is to make the 'if' statement to catch any user-input with alphabet, except the "giveup" line

here is the code that I built:

if [ $usrGuess =~ *[:alpha:]* && $usrGuess != "giveup" ]

once I run the code, it gives out the error message saying that:

[: missing `]'

If you guys have any solution to this, I will be happy to hear your advice :)

Thanks!