vendredi 1 mai 2020

How to return True when detecting a nested list of empty lists?

I was trying to return the True value when writing the statement: list = [[],[],[],[]] list == [] Instead it returns False

My nested list consists of a variable number of empty lists. How do I write a single statement that could apply to nested list of [1/2/3...] empty lists?

Is there a way to understand if a SASS variable is a valid color?

I would like to know if a variable is a valid color in a SASS function. This is the first code I wrote, so I check if the variable is initialised.

@function get-valid-color($color) {
  @if variable-exists($color) {
    @return $color;
  } @else {
    @return inherit;
  }
}

I would like to understand also if the variable is a valid color or not. Only in that case I would return the variable, otherwise I would return inherit.

This the expected output:

  $primary-text = red;
  $primary-bg = #000000;
  $border-width = 4px;

  color: get-color($border-width);           // color: inherit;
  background-color: get-color($primary-bg);  // background-color: #000000;
  color: get-color($primary-text);           // color: red;
  color: get-color($fake-variable);          // color: inherit

Ideas, thanks.

Is there a way to use the get accessor to find out what row and column the element you want to return from an multidimensional array are?

I'm fairly new to C# and I'm trying to understand certain things by testing different kinds of things. I just learned about get and set accessors and I want to try and utilize them as much as possible to avoid having to write a lot of get and set functions (which might not be unavoidable I guess). I want to run an if-statement in one of my getters and in that if-statement I want to do different things depending on the row and column in a multi-dimensional array, is there a way to know what row and coloumn I am trying to access?

Here's an example of the code I currently have and functionality I would like to have:

//What I have
private int[,] _array;
public int[,] Array {get => _array; set => _array = value; }

//Functionality I want to have
if(x_row < 0 || y_column < 0)
{
    return something;
}
else
{
    return something_else;
}

So depending on the row and column I would want to return different values. Is there a way to do that using accessors or am I better of just writing a get function?

Thanks!

If statement that checks if personal number has the expected number of digits//PYTHON

So i have to write a code that checks if personal number has 13 digits,if it has more or less it must raise ValueError,so i tried smth like this but it isnt working as intended

def jmbg(self,value):
    if len(str(value)) < 13 and len(str(value)) > 13:
        raise ValueError("Personal number must have 13 digits!")
    self.__jmbg = value

Hide button for modal if empty

I'm currently working on a web interface for Excel reports relating to stock in a warehouse using Flask. I've built a table using DataTables that pulls data from a csv file and it's working away. I've been asked to add in an extra data cell at the end of each row which has a clickable button to open notes if a note exists on the csv file for that stock item. I'm trying to use a Font Awesome icon for the user to click to open a modal which shows the note however I cannot get the icon to only appear when there is a note to view.

Here's what I've tried

<tbody>                     
    

                            
</tbody>

And here is the jQuery I've tried -

$("#notes").find('hide_show').filter(function() {
return !this.firstChild; 
}).hide();

I have based my attempt off this answer --> Hide div's if they are empty

The other issue I have is that when the modal opens, it shows the same note (the note for stock item number 1) for each stock item regardless.

How do I get the icon to only appear when there is a note and then get the correct note to appear in the modal when opened?

Any help would be greatly appreciated! Thanks

PHP Quiz Radio and Checkbox Calculation

I'm designing a simple quiz with PHP and would like to know if i'm approaching it the correct way before proceeding.

The quiz will have approximately 25 questions, a mixture of radio buttons and checkboxes. The idea is to calculate the total and display this to the user when the quiz is submitted.

I have just four questions so far. Questions 1 - 3 are radio buttons, one selection max. Question 4 is a checkbox and allows two selections max, each correct selection is worth 0.5

Here's a snippet of my html code (question and answer text removed).

HTML

// q1 answer is value 2
<input type="radio" name="form[1-1]" value="1">
<input type="radio" name="form[1-1]" value="2">
<input type="radio" name="form[1-1]" value="3">

// q2 answer is value 1
<input type="radio" name="form[1-2]" value="1">
<input type="radio" name="form[1-2]" value="2">
<input type="radio" name="form[1-2]" value="3">

// q1 answer is value 2
<input type="radio" name="form[1-3]" value="1">
<input type="radio" name="form[1-3]" value="2">
<input type="radio" name="form[1-3]" value="3">

// q4 answer is value 1 or 3. 0.5 points each
<input type="checkbox" name="form[1-4][]" value="1">
<input type="checkbox" name="form[1-4][]" value="2">
<input type="checkbox" name="form[1-4][]" value="3">
<input type="checkbox" name="form[1-4][]" value="4">

The PHP code below works, is it the correct approach or is there a more efficient way? Specifically with the checkbox question 4.

PHP

$total = array();

$total = '0';

$q1 = $_POST['form']['1-1'];
$q2 = $_POST['form']['1-2'];
$q3 = $_POST['form']['1-3'];
$q4 = $_POST['form']['1-4'];

// answer with value 2 is correct
if ($q1 == '2' ) {
    $total++;
};
// answer with value 1 is correct
if ($q2 == '1' ) {
    $total++;
};
// answer with value 2 is correct
if ($q3 == '2' ) {
    $total++;
};
// answer with value 1 is correct
if ($q4[0] == '1' ) {
    $total = $total + 0.5;
};
// answer with value 3 is correct
if ($q4[1] == '3' ) {
    $total = $total + 0.5;
};

// send $total to database here

I'm not looking to use JS / Jquery, I want to use a PHP approach.

Can't use if condition in JavaScripts (userscript)

I feel dumb for that question. I know how use if condition in bash and there are some differences that ruins my code. In bash you can write * character to show not care what there writen,^ for protocol and =~ for approximately equal. Example:

url = https://en.m.wikipedia.org/wiki/Solovetsky_Stone
if [[ $url =~ ^.*.wikipedia.org/wiki/* ]]

It find all wiki page in any language and in mobile mode or not. I don't know how do that in javascript. I can't use string1.localeCompare(string2) because it show only same (0) and aa (1,-1). I want run script in specific page via comparing url in userscript (javascript).

Sorry for my english.