mardi 28 avril 2020

To understand %*d and if (( scanf( "%d", &n ) != 1 ) || ( n <= 0 )) in c

I have to print this pattern in c

4 4 4 4 4 4 4

4 3 3 3 3 3 4

4 3 2 2 2 3 4

4 3 2 1 2 3 4

4 3 2 2 2 3 4

4 3 3 3 3 3 4

4 4 4 4 4 4 4

And I found this code associated with it

#include <stdio.h>
#include <limits.h>
#include <stdlib.h>

int main()  
{ 
    while ( 1 ) 
    { 
        printf( "Enter a non-negative number (0 - exit): " ); 

        int n; 

        if ( ( scanf( "%d", &n ) != 1 ) || ( n <= 0 ) ) break; 

        if ( INT_MAX / 2 < n )  
        { 
            n = INT_MAX / 2; 
        } 

        int width = 1; 

        for ( int tmp = n; tmp /= 10; ) ++width; 

        putchar( '\n' ); 

        int m = 2 * n - 1; 

        for ( int i = 0; i < m; i++ ) 
        { 
            for ( int j = 0; j < m; j++ ) 
            {
                int value1 = abs( n - i - 1 ) + 1;
                int value2 = abs( n - j - 1 ) + 1;

                printf( "%*d ", width, value1 < value2 ? value2 : value1 );
            } 
            putchar( '\n' ); 
        } 

        putchar( '\n' ); 
    } 

    return 0; 
 }

I want to know why in this statementscanf( "%d", &n ) != 1 is used

if (( scanf( "%d", &n ) != 1 ) || ( n <= 0 ));

and also how single format specifier is accepting two values here

printf( "%*d ", width, value1 < value2 ? value2 : value1 );

Why % and * are used together"%*d"??

values in this csv are not being edited - Powershell

$users = Import-Csv -Path "C:\scripts\door-system\test\testChange.csv" -Encoding UTF8 $users | ft

$output = forEach ($user in $users) {

if ($user.GroupName -like "Normal")
{
    $output.GroupName = "edited"
}

}

$output | export-csv .\modified.csv -noTypeInformation

if condition is being executed even after inside condition function with parameter returns false

bool isCycle(vector <int> adj[],int v)
{
    vector <bool> visited(v, false);
    for (int u = 0;u < v;u++)
    {
        if (visited[u] == false)
        {
           if(bfs(u, adj, visited,v));
                return true;
        }
    }
    return false;
}

even after function inside if condition returned false. Why is it executing the if condition ???

if(bfs(u, adj, visited,v));
       return true;

lundi 27 avril 2020

my c code not counting properly word and sentences, but counting char

I am making a C program to count No. of Letters , Words & sentences. but if condition for counting words and Sentences doesn't checks for null character. can anyone help me what i am doing wrong? However if condition for counting no of char is checking for null character.

#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <ctype.h>

int main (void)
{
    string text = get_string("text: ");
    int p=0,q=0,r,j=0,k=0;
    {
        printf("Your Text is : %s\n", text);
    }
    for(p=0,r=strlen(text); p<r; p++)
    {
        // for counting Letters.
        if (text[p]!= ' ' && text[p]!= '\0' && text[p]!='-' && text[p]!='_' && text[p]!= '.')
        {
            q++;
        }
        // for counting Words.
        else if (text[p]==' ' || text[p]== '-' || text[p]== '\0'  || text[p]=='_')
        {
            j++;
        }
        // for counting Sentences.
        else if (text[p]== '.' || text[p]== '!' || text[p]== '\0')
        {
            k++;
        }
    }
        printf("no.of chars is %i\n",q);
        printf("no.of words is %i\n",j);
        printf("no.of sentences is %i\n",k);
}

included cs50 Library to get string input

IF..ELSE Logic in stored procedure

I have a stored procedure where I need to pull data from a table column based on an input parameter.

If the input parameter is 'Y' I need to pull only 'Y' values from the table column, if it is 'N', I need to pull all values irrespective of 'Y' or 'N' or NULL.

if else on node.textContent

I am trying to make a drag and drop quiz in javascript that is able to check the answers after they are dropped in and give feedback on the answer. So far I have made the drag and drop section but when trying to check answers I am running into problems. This is my code for the webpage:

<!DOCTYPE html>
<html>
<head>
<title>Drag n Drop DEMO</title>
</head>

<body>
<h3>Quiz!</h3>
<p>Possible Answers</p>
<ol ondragstart="dragStartHandler(event)" ondragend="dragEndHandler(event)">
 <li draggable="true" data-value="geolocation">Geolocation</li>
 <li draggable="true" data-value="canvas">Canvas</li>
 <li draggable="true" data-value="web-worker">Web Workers</li>
 <li draggable="true" data-value="appcache">AppCache</li>
 <li draggable="true" data-value="drag-and-drop">Drag and Drop</li>

</ol>

<script>
  var internalDNDType = 'text/x-example'; // set this to something specific to your site
  function dragStartHandler(event) {
    if (event.target instanceof HTMLLIElement) {
      // use the element's data-value="" attribute as the value to be moving:
      event.dataTransfer.setData(internalDNDType, event.target.dataset.value);
      event.dataTransfer.effectAllowed = 'move'; // only allow moves
    } else {
      event.preventDefault(); // don't allow selection to be dragged
    }
  }
   function dragEndHandler(event) {
    if (event.dataTransfer.dropEffect == 'move') {
      // remove the dragged element
      event.target.parentNode.removeChild(event.target);
    }
  }
</script>

<p>Provides scripts with a resolution-dependent bitmap canvas, which can be used for rendering graphs, game graphics, art, or other visual images on the fly.</p>
<ul id="answer1" ondragenter="dragEnterHandler(event)" ondragover="dragOverHandler(event)"
    ondrop="dropHandler(event)">
&nbsp;
</ul>

<p>Move (an image or highlighted text) to another part of the screen using a mouse or similar device.</p>
<ul id="answer2" ondragenter="dragEnterHandler(event)" ondragover="dragOverHandler(event)"
    ondrop="dropHandler(event)" style="margin-top: 0px;">
&nbsp;
</ul>

<p>Allows a developer to specify which files the browser should cache and make available to offline users.</p>
<ul id="answer3" ondragenter="dragEnterHandler(event)" ondragover="dragOverHandler(event)"
    ondrop="dropHandler(event)">
&nbsp;
</ul>

<p>Provides web applications with access to geographical location data about the user’s device.</p>
<ul id="answer4" ondragenter="dragEnterHandler(event)" ondragover="dragOverHandler(event)"
    ondrop="dropHandler(event)">
&nbsp;
</ul>

<p>Allows JavaScript to execute scripts in the background.</p>
<ul id="answer5" ondragenter="dragEnterHandler(event)" ondragover="dragOverHandler(event)"
    ondrop="dropHandler(event)">
&nbsp;
</ul>

<script>
  var internalDNDType = 'text/x-example'; // set this to something specific to your site
  function dragEnterHandler(event) {
    var items = event.dataTransfer.items;
    for (var i = 0; i < items.length; ++i) {
      var item = items[i];
      if (item.kind == 'string' && item.type == internalDNDType) {
        event.preventDefault();
        return;
      }
    }
  }
  function dragOverHandler(event) {
    event.dataTransfer.dropEffect = 'move';
    event.preventDefault();
  }
  function dropHandler(event) {
    var li = document.createElement('li');
    var data = event.dataTransfer.getData(internalDNDType);
    if (data == 'canvas') {
      li.textContent = 'Canvas';
    } else if (data == 'drag-and-drop') {
      li.textContent = 'Drag and Drop';
    } else if (data == 'appcache') {
      li.textContent = 'AppCache';
    } else if (data == 'geolocation') {
      li.textContent = 'Geolocation';
    } else if (data == 'web-worker') {
      li.textContent = 'Web Workers';
    }else {
      li.textContent = 'Unknown Answer';
    }
    event.target.appendChild(li);
  }
</script>

<h4>Check Answers Here!</h4>
<button type="button" onclick="checkAns()">Check Answers</button>&nbsp;&nbsp;<button onClick="window.location.reload();">Reset</button>
<br>
<div id="div1"></div>
<script>
function checkAns() {
    var node1 = document.getElementById('answer1')
    htmlContent = node1.innerHTML
    textContent = node1.textContent;

    var node2 = document.getElementById('answer2')
    htmlContent = node2.innerHTML
    textContent = node2.textContent;

    var node3 = document.getElementById('answer3')
    htmlContent = node3.innerHTML
    textContent = node3.textContent;

    var node4 = document.getElementById('answer4')
    htmlContent = node4.innerHTML
    textContent = node4.textContent;

    var node5 = document.getElementById('answer5')
    htmlContent = node5.innerHTML
    textContent = node5.textContent;


    var displayAnswer1 = document.createElement("p")   
    if (node1.textContent === 'Canvas') {
        var textNode1 = document.createTextNode("Question 1 was correct.");
        displayAnswer1.appendChild(textNode1);
        var bindElement1 = document.getElementById("div1");
        bindElement1.appendChild(displayAnswer1);
    } 
      else {
      var textNode1 = document.createTextNode("Question 1 was incorrect.");
      displayAnswer1.appendChild(textNode1);
      var bindElement1 = document.getElementById("div1");
      bindElement1.appendChild(displayAnswer1);
    }
}
</script>
</body>
</html>

I am able to get the textContent from the drag and drop section and even when I type alert(node1.textContent); I get the correct answer from the drag and drop. But in my if else statement whenever I press the button the if statements condition is never met. The condition I have in there currently is if (node1.textContent === 'Canvas') which is what it comes out to when I sat it as an alert instead of an if else. Any help would be much appreciated!

UPDATE column if column is greater than a value

I have a table with these columns

  • proxy_id (primary key)
  • proxy (IP of a proxy)
  • current_requests (number of current requests the proxy has handled since its last_cool_down)
  • total_requests (number of requests this proxy has fulfilled in its lifetime)
  • last_used (datetime of last time this proxy was used)
  • last_cool_down (datetime of last time this proxy was on cooldown)

After I have used a proxy I run this command (obviously with the current date and time, but we will assume the current datetime is 2020-04-28 13:10:03)

UPDATE proxy_table
SET
current_requests = current_requests + 1,
total_requests = total_requests + 1,
last_used = '2020-04-28 13:10:03',
last_cool_down = '2020-04-28 13:10:03'
WHERE proxy_id = 1

My issue is I must ALWAYS update current_requests and last_used no matter what. However, in the above code, it doesn't consider this condition. If current_requests + 1 == 20, then current_requests = 0 and last_cool_down = '2020-04-28 13:10:03'. How can I do this?

So essentially something like this

UPDATE proxy_table

if (current_requests + 1 == 20) {
    SET current_requests = 0;
    SET last_cool_down = '2020-04-28 13:10:03';
else {
    SET current_requests = current_requests + 1;
}

SET total_requests = total_requests + 1;
SET last_used = '2020-04-28 13:10:03';
WHERE proxy_id = 1;

I want to do this in one sql statement, rather than running 2 statements. My attempt at this didn't work:

UPDATE proxy_table
SET
    CASE
        WHEN current_requests + 1 = 20 THEN current_requests = 0, last_cool_down = '2020-04-28 13:10:03' ELSE current_requests = current_requests + 1
    total_requests = total_requests + 1,
    last_used = '2020-04-28 13:10:03'
WHERE
proxy_id = 1