responce to multi numbers - node.js

im trying to make a specific responce for a set of numbers but i dont know the exact code for that for exemple i want the responce for only numbers in between 10 and 15 it doesnt interupt with the other 2 results
module.exports.run = async (bot, message, args) => {
var s, final;
var random = Math.floor(Math.random() *( 10000)) /500;
s = random + .005 + '',
final = s.substring(0, s.indexOf('.') + 3);
message.reply("Your BC Mark is " + final);
if (final + 16 > 16){
message.reply(`excellent mark !`)
}
if (final + 10 > 15 ){
message.reply(`good !`)
}
if (final + 1 < 10.5) {
message.reply(`not bad !`)
}
}
module.exports.help = {
name: "bac"
}

You could check if your number is greater than or equal to a value and also less than or equal to a value. This would create your intended ranges.
Test the code below and implement it as necessary.
const num = 8;
if (num >= 1 && num <= 5) console.log('Between 1 and 5.');
if (num >= 6 && num <= 10) console.log('Between 6 and 10.');
if (num >= 11 && num <= 15) console.log('Between 11 and 15.');
More about comparison operators.

Related

cs50 Credit issue with identifying card company

I'm completely new to programming and have been working through pset1 this week but credit is proving to be a bit tough. I've managed to get the checksum working but I'm having real trouble identifying which company the card belongs to, the last part of the code seems to introduce more errors every time I try to fix it. I would really appreciate it if anybody could point me in the right direction.
#include <cs50.h>
#include <stdio.h>
int main (void)
{
long long ccnumber;
do
{
ccnumber = get_long_long("Credit card number: ");
}
while (ccnumber < 0);
long long cc = ccnumber;
int s1;
while (cc > 0)
{
int l1 = cc % 10;
s1 = s1 + l1;
cc = cc / 100;
}
cc = ccnumber / 10;
int s2;
while (cc > 0)
{
int l1 = cc % 10;
l1 = l1 * 2;
if (l1 > 9) l1 = l1 % 10 + l1 / 10;
s2 = s2 + l1;
cc = cc / 100;
}
int s3 = s1 + s2;
if (s3 % 10 == 0)
s3 = true;
long long n = ccnumber;
int count = 0;
while (n != 0)
{
n /= 10;
++count;
}
int twonumbers = ccnumber;
while(twonumbers >= 100)
{
twonumbers = twonumbers / 10;
}
int firstnumber = ccnumber;
while(firstnumber >= 10)
{
firstnumber = firstnumber / 10;
}
if (twonumbers == 34 || 37 && (count == 15 ))
printf("AMERICAN EXPRESS\n");
else if (twonumbers == 51 || 52 || 53 || 54 | 55 && (count == 16 ))
printf("MASTER CARD\n");
else if (firstnumber == 4 && (count == 13 || 16 ))
printf("VISA\n");
else
printf("INVALID\n");
}
Your ending if statements are not correct:
if (twonumbers == 34 || 37 && (count == 15 ))
...
should be
if ((twonumbers == 34 || twonumbers == 37) && (count == 15 ))
And the same for each if statement - you have to repeat what is being compared for every or statement or the compiler will assume "true" as in this case, 37 is no equal to 0.
Not, you can also compare ranges as well:
else if (twonumbers >= 51 && twonumbers <= 55 && count == 16)
Note I also correct a binary or there on the last compare (you had only 1 | and not 2 ||)
The last is
else if (firstnumber == 4 && (count == 13 || count == 16 ))
I also question what:
int s3 = s1 + s2;
if (s3 % 10 == 0)
s3 = true;
is doing as this will actually always be "true" in a C sense, but in reality, you don't actually use s3 anywhere so I am confused.
Lastly, turn on all compiler warning. It would have told you about a lot of the things I mention here.
Now, I am not saying that this is the only thing wrong here, but it was the most obvious.

CS50 Credit: Always getting INVALID return

Was attempting Credit from CS50 and kept getting INVALID return from my code. I approached this problem by using arrays even though it may not have been the best method. Code compiles with no issues.
My pseudocode logic was:
1) obtain card number
2) use loop to find number of digits
3) check if card contains 13, 15 or 16 digits
4) if so, write digits from long into array
5) have a copy of original array to multiply every other number by 2
6) add the digits of the product
7) check for card length and starting digits
Here is my code:
#include <stdio.h>
#include <cs50.h>
int main(void)
{
// Get credit card number
long num = get_long("Number: ");
// Find number of digits
int digits = 0;
while (num > 0)
{
num /= 10;
digits++;
}
// Check if number of digits is within possible range
if (digits != 13 && digits != 15 && digits != 16)
{
printf("INVALID\n");
}
int originalnumber[digits];
// Write each digit of credit card number into an array
for (int i = digits - 1; i >= 0; i--)
{
originalnumber[i] = num % 10;
num = num / 10;
}
// Multiply alternate digits by 2
int number[digits];
for (int i = 0; i < digits; i++)
{
number[i] = originalnumber[i];
}
for (int i = 1; i < digits; i+=2)
{
number[i] = number[i] * 2;
}
// Add product digits
int sum = 0;
int temp;
for (int i = 0; i < digits; i++)
{
temp = (number[i] % 10) + ((number[i] / 10) % 10);
sum = sum + temp;
}
// Check for card length and starting digits
// AMEX
if (digits == 15)
{
if (originalnumber[14] == 3 && sum % 10 == 0 && (originalnumber[13] == 4 || originalnumber[13] == 7))
{
printf("AMEX\n");
return 0;
}
}
// MasterCard
if (digits == 16)
{
if (originalnumber[15] == 5 && sum % 10 == 0 && (originalnumber[14] == 1 || originalnumber[14] == 2 || originalnumber[14] == 3 || originalnumber[14] == 4 || originalnumber[14] == 5))
{
printf("MASTERCARD\n");
return 0;
}
}
// Visa
if (digits == 13)
{
if (originalnumber[12] == 4 && sum % 10 == 0)
{
printf("VISA\n");
return 0;
}
}
if (digits == 16)
{
if (originalnumber[15] == 4 && sum % 10 == 0)
{
printf("VISA\n");
return 0;
}
}
printf("INVALID\n");
return 1;
}
I tried debug50 and it seems that when I try to sum the digits together using temp and sum, the loop completes with sum still being 0. May I know what is wrong here? Is the flow of my pseudocode wrong or are there any glaring mistakes that I may have overlooked? (stared at this for way too long..)
Thank you in advance!
If sum is always 0, regardless of whether that is what you expect, sum % 10 would always be 0, so that is not the "false" that is failing the tests.
Which should direct your attention to originalnumber.
What is the value of num after this loop?
while (num > 0)
{
num /= 10;
digits++;
}

How should I fix my infinite while loop that takes in 3 conditions? Also stylistic questions for novice

writing code to test the Hailstone Sequence, also called Collatz conjecture. Code will print out the number of iterations of the Hailstone sequence.
def main():
start_num = eval (input ("Enter starting number of the range: "))
end_num = eval (input ("Enter ending number of the range: "))
The main problem is that my code returns an infinite loop. I want to check all of these conditions in one statement
while (start_num > 0 and end_num > 0 and end_num > start_num):
cycle_length = 0
max_length = 0
max_number = 0
my code seems inefficient, there is probably a better way to approach the problem
for i in range(start_num, (end_num + 1)):
cycle_length = 0
while (i != 1):
if (i % 2 == 0):
i = i // 2
cycle_length += 1
if (i % 2 == 1):
i = ((3 * i) + 1)
cycle_length += 1
print (cycle_length)
I just started coding, and I always know that there is a more efficient way to approach these problems. Any suggestions on methodology, problem solving, or stylistic advice would be greatly appreciated.
Here is an answer in java. I assume that we will not start with 1.
public static void main(String[] args) {
int counter =0;
Scanner sc = new Scanner(System.in);
System.out.println("Give us a number to start with:");
int start = sc.nextInt();
System.out.println("Give us a number to end with:");
int end = sc.nextInt();
if (end > start) {
for (int i = 0; i <= end - start; i++) {
counter = 0;
int num = start + i;
int temp = num;
while(temp != 1) {
if ( temp % 2 == 0 ) {
temp = temp / 2;
} else {
temp = 3* temp +1;
}
counter++;
}
System.out.println(num + " takes " + counter + "iterations.");
}
} else {
System.out.println("Your numbers do not make sense.");
}
}
Here's an answer in python in case you're staying up late trying to solve this problem. :P Have a good night.
start_num = 1
end_num = 10
for i in range(start_num, (end_num + 1)):
cycle_length=0
num = i
while (num != 1):
if (num % 2 == 0):
num = num // 2
cycle_length+=1
else:
num = ((3 * num) + 1)
cycle_length+=1
print(cycle_length)

adding strings as numbers

I'm trying to add together two large numbers, stored as strings.
Here's what I have so far:
function addBigNums(a,b){
c = ""; // output
o = 0; // carryover
startLen = a.length-1;
for(i = startLen; i >= 0; i--) {
sum = parseInt(a[i], 10) + parseInt(b[i], 10) + o;
c = (sum % 10) + c;
o = sum >= 10;
}
if(o === true) c = "1" + c;
return c;
}
I'm running into two issues:
1 ) my carry is not always functioning properly, primarily when:
2 ) the numbers length differ.
Right now I think I would have to prepend 0's onto the shorter number in order to get this to function as expected.
Any better alternatives to this?
Simple, straightforward integer addition like you would do it manually:
a = "123456"; // input a
b = "123456"; // input b
c = ""; // target-string
o = 0; // overflow-bit
// traverse string from right to left
for(i = a.length - 1; i >= 0; i--) {
// do the calculation (with overflow bit)
sum = parseInt(a[i]) + parseInt(b[i]) + o;
// prepend resulting digit to target
c = (sum % 10) + c;
// set overflow bit for next round
o = sum >= 10;
}
// prepend another "1" if last overflow-bit is true
if(o == true) c = "1" + c;
If strings a and b are not equal length (but you stated that they are), you should prepend the shorter string with zeros before calculation.
Consider both numbers to be an array of digits. Add them up right-to-left handling overflow flag. Demo. Assuming your numbers are of the same length
function getNumber(len) {
return Array.apply(null, new Array(len)).map(function(){
return Math.floor(Math.random()*9);
}).join('');
}
var len = 600,
a = getNumber(len), //use your numbers here
b = getNumber(len),
flag = 0;
var c = [].reduceRight.call(a, function(acc, val, idx) {
val = +val + (+b.charAt(idx)) + flag;
flag = val / 10 | 0;
val %= 10;
return val + acc;
}, '');
c = (flag ? 1: '') + c;
console.log(a, b, c);

Algorithm for finding continuous repeated sequences

I'm looking for an algorithm that finds short tandem repeats in a genome sequence.
Basically, given a really long string which can only consist of the 4 characters 'ATCG', I need to find short repeats between 2-5 characters long that are next to each other.
ex:
TACATGAGATCATGATGATGATGATGGAGCTGTGAGATC
would give ATGATGATG or ATG repeated 3 times
The algorithm needs to scale up to a string of 1 million characters so I'm trying to get as close to linear runtime as possible.
My current algorithm:
Since the repeats can be 2-5 characters long, I check the string character by character and see if the Nth character is the same as the N+Xth character, with X being 2 through 5. With a counter for each X that counts sequential matches and resets at a mismatch, we know if there is a repeat when X = the counter. The subsequent repeats can then be checked manually.
You are looking at each character which gives you O(n), since you compare on each character the next (maximum) five characters this gives you a constant c:
var data = get_input();
var compare = { `A`, `T`, `G`, `A`, `T` } // or whatever
var MAX_LOOKAHEAD = compare.length
var n
var c
for(n = data_array.length; n < size; i++) { // Has runtime O(n)
for(c = 0; c < MAX_LOOKAHEAD; c++) { // Maximum O(c)
if( compare[c] != data[i+c] ) {
break;
} else {
report( "found match at position " + i )
}
}
}
It is easy to see that this runs O(n*c) times. Since c is very small it can be ignored - and I think one can not get rid of that constant - which results in a total runtime of O(n).
The good news:
You can speed this up with parallelization. E.g. you could split this up in k intervals and let multiple threads do the job for you by giving them appropriate start and end indices. This could give you a linear speedup.
If you do that make sure that you treat the intersections as special cases since you could miss a match if your intervals split a match in two.
E.g. n = 50000:
Partition for 4 threads: (n/10000) - 1 = 4. The 5th thread won't have a lot to do since it just handles the intersections which is why we don't need to consider its (in our case tiny) overhead.
1 10000 20000 40000 50000
|-------------------|-------------------|-------------------|-------------------|
| <- thread 1 -> | <- thread 2 -> | <- thread 3 -> | <- thread 4 -> |
|---| |---| |---|
|___________________|___________________|
|
thread 5
And this is how it could look like:
var data;
var compare = { `A`, `T`, `G`, `A`, `T` };
var MAX_LOOKAHEAD = compare.length;
thread_function(args[]) {
var from = args[0];
var to = args[1];
for(n = from ; n < to ; i++) {
for(c = 0; c < MAX_LOOKAHEAD; c++) {
if( compare[c] != data[i+c] ) {
break;
} else {
report( "found match at position " + i )
}
}
}
}
main() {
var data_size = 50000;
var thread_count = 4;
var interval_size = data_size / ( thread_count + 1) ;
var tid[]
// This loop starts the threads for us:
for( var i = 0; i < thread_count; i++ ) {
var args = { interval_size * i, (interval_size * i) + interval_size };
tid.add( create_thread( thread_function, args ) );
}
// And this handles the intersections:
for( var i = 1; i < thread_count - 1; i++ ) {
var args = { interval_size * i, (interval_size * i) + interval_size };
from = (interval_size * i) - compare.length + 1;
to = (interval_size * i) + compare.length - 1;
for(j = from; j < to ; j++) {
for(k = 0; k < MAX_LOOKAHEAD; k++) {
if( compare[k] != data[j+k] ) {
break;
} else {
report( "found match at position " + j )
}
}
}
}
wait_for_multiple_threads( tid );
}

Resources