Wednesday, April 26, 2023

Coding Challenge #54 C++ int to std::string (no stringstream or to_string())

Gets a string from an integer (ejemplo gratis: 123 -> "123")

Wanted to come up with my own function for this like 10 years ago but couldn't think of a way and used stringstream. Now after all the binary & bitwise stuff I've been doing, the answer easily popped into my head when the topic of getting the last digit in a number came to mind.

 

 // get lowest digit (of base-10 format number)
int getLowestDigit(int num){
    int lowest_digit = num % 10;
    return lowest_digit;
}

// convert number to string (base-10 representation) in probs a super inefficient way
std::string numToString(int num){
    std::string str = "";
    while(num > 0){
        int digi = num % 10; // current right-most digit
        str += '0' + digi;
        num /= 10; // move number over by one base-10 digit's worth
    }
    
    // above number string is backwards. flip it
    std::string fin = "";
    for(int xint = fin.length() - 1;xint > -1;xint--){
        fin += str[xint];
    }
    
    return fin;
}

Sunday, April 16, 2023

Coding Challenge #53 Combinations

 // the math formula (in my dialect): x = n! / ((n-r)!r!)

// finds factorial of a number
function fac(x){
    var done = x;
    while(x > 1) done *= --x;
    return done;
}

// finds combinations given n = total number of options, and r = slots available to be filled with options.
function combo(n, r){
    return fac(n) / (fac(n-r) * fac(r));
}

console.log("Combinations for 3 elected positions, with 6 candidates = " + combo(6, 3));

Saturday, April 15, 2023

Coding Challenge #52 Factorial (Recursive)

 // a recursive version of a function to find a factorial of a number
// a factorial of a number being the product of that number multiplied by all numbers between itself and 1 (e.g. 3! = 3 * 2 * 1. 4! = 4 * 3 * 2 * 1.)
function fac_rec(x){
    if(x <= 1) return 1;
    else return x * fac_rec(x-1);
}

Friday, April 14, 2023

Coding Challenge #51 Factorial (Loop)

 // finds factorial of a number
function fac(x){
    var done = x;
    while(x > 1) done *= --x;
    return done;
}

Thursday, April 13, 2023

Coding Challenge #50 Permutations

 // a function to calculate the permutations given total possibilities and events to calculate for.
// for example, in a race, if we want to know total possible permutations for who comes in 1rst & 2nd, and we have 4 racers, then that'd be cp(4,2) = 12
// in order for the result to be accurate and meaningful, the eventsToCalcFor must be <= totalPossibilities
function cp(totalPossibilities, eventsToCalcFor){
    var fin = totalPossibilities;
    for(var xint = 1;xint < eventsToCalcFor;xint++){
        fin *= totalPossibilities-xint;
    }
    return fin;
}

console.log("24 racers can come into 1rst, 2nd, and 3rd places how many different ways? Answer: " + cp(24, 3));

console.log("I can organize my green, blue, and orange socks how many different ways? Answer: " + cp(3, 3));

Wednesday, April 12, 2023

Coding Challenge #49 Number Predicter - Version 3

 /* version 3
Again slightly more useful & accurate than version 2 in that this version will try a little harder for when the supplied time is between the min & max.
I noticed in version 2 that f(1993.5) returned < than f(1993). Odd. This version, when supplied 1993.5, will grab values for the closest surrounding dataset values (1993 & 1994) and interpolate linearly between those two specifically.
*/

/* version 3 notes (on getting the between the indices)
4.5 // input time to predict
4, 7 // range times
100, 150 // range values
7-4 = 3 // time diff
150 - 100 = 50 // value diff
50/3 = 16.6xxxx // per value value

100 + ((4.5 - 4) * 16.6xxxx)
minV + ((input - minT) * perYear)
*/

var data = [{year:1992,cost:1440},
{year:1993,cost:1560},
{year:1994,cost:1620},
{year:1995,cost:1780},
{year:1996,cost:1860},
{year:1997,cost:1920},
{year:1998,cost:1940},
{year:1999,cost:1999},
{year:2000,cost:2040}];
var data2 = [];
for(var xint = 0;xint < 10;xint++)
    data2.push({year:2000+xint,pay:10+(xint/10)});
// then do one for #pets compared to #renkou ppl in household

// data is the cost of a used car with x miles on it in a certain year; goal: given a year, predict the cost that year
// data2 is hourly pay each year starting 2000 $10.00/hr. Goal: given a year, predict the pay that year


// assumes array is sorted least to greatest
function getNearIdxs(ary, val){ // returns object with 2 valid indices (assuming ary was not empty)
    if(ary.length < 2) return {min:0,max:0};
    var out = {min:0,max:0};
    for(var xint = 0,len=ary.length;xint < len;xint++){
        if(val < ary[xint]){
            out.max = xint;
            out.min = Math.max(0,xint-1);
            return out;
        }
    }
    return out;
}

// where timeName is the variable per object with the time value (the consistent change over time)
// and valueName is the value we're trying to find given a time
// .predict(v): returns a predicted linear output
// Note: 2 same .times[] values (like year 1993, 1993, 1994) would confuse it and produce probably undesirable output
function reader(data, timeName, valueName){
    this.times = [];
    this.values = [];
    this.minT = 1; // minimum time
    this.maxT = 1;
    this.minV = 1; // minimum value
    this.maxV = 1;
    this.diffT = 1;
    this.diffV = 1;
    this.perYear = 1; // final linear quotient
    
    // sort the data so we can easily grab the closest values by indices (least to greatest)
    data.sort((a,b,tname)=>{return a[tname]>b[tname];});
    this.d = data;
    
    // now let's try to make a mini model
    this.minT = data[0][timeName];
    this.maxT = data[0][timeName];
    this.minV = data[0][valueName];
    this.maxV = data[0][valueName];
    for(var xint = 0;xint < data.length;xint++){
        this.minT = Math.min(this.minT, data[xint][timeName]);
        this.maxT = Math.max(this.maxT, data[xint][timeName]);
        this.minV = Math.min(this.minV, data[xint][valueName]);
        this.maxV = Math.max(this.maxV, data[xint][valueName]);
        this.times.push(data[xint][timeName]);
        this.values.push(data[xint][valueName]);
    }
    this.diffT = Math.max(1,this.maxT - this.minT);
    this.diffV = Math.max(1,this.maxV - this.minV);
    this.perYear = this.diffV / this.diffT; // cost per year linear avg
    
    this.predict = function(time){
        if(time > this.maxT){
            return this.maxV + ((time-this.maxT) * this.perYear);
        }else if(time < this.minT){
            return this.minV - ((this.minT-time) * this.perYear);
        }else{ // within range
            var found = false;
            var val = 0;
            for(var xint = 0;xint < this.times.length;xint++){
                if(this.times[xint] == time){
                    found = true;
                    val = this.values[xint];
                    break;
                }
            }
            if(found){
                return val; // found in defined dataset
            }else{
                // estimate
                var x = getNearIdxs(this.times, time);
                var diffT = this.times[x.max] - this.times[x.min];
                var diffV = this.values[x.max] - this.values[x.min];
                if(diffT == 0) diffT = 1;
                var vPerT = diffV / diffT;
                return this.values[x.min] + ((time - this.times[x.min]) * vPerT);
            }
        }
    }
}

var r1 = new reader(data, 'year', 'cost');
console.log(r1.predict(1992));
console.log(r1.predict(1993));
console.log(r1.predict(1994));
console.log(r1.predict(1993.5));
console.log(r1.predict(1993.1));
console.log(r1.predict(1993.9));

var r2 = new reader(data2, 'year', 'pay');
console.log(r2.predict(2001));

Tuesday, April 11, 2023

Coding Challenge #48

submitted for Coursera quiz. Read int's from a file, and calculate their average

#include <stdio.h>
int main(){
    FILE *filePointer; // this is the pointer to the file
    int buffer[1000]; // file content
    int safety = 10000;
    int count = 0;
    filePointer = fopen("file.tsv", "r"); // open the file with file pointer to the file
    while(fscan(filePointer, "%d\t", &buffer[count]) && --safety){
        count++;
    } // read the file the file pointer file
    fclose(filePointer); // close the file pointer to the file
    int total = 0; // total the weight
    for(int xint = 0;xint<count;xint++)total+=buffer[xint]; // it make the total the total to make the average
    int average=total/count;
    printf("average:%d",count);
}

Coding Challenge #54 C++ int to std::string (no stringstream or to_string())

Gets a string from an integer (ejemplo gratis: 123 -> "123") Wanted to come up with my own function for this like 10 years ago ...