imask.js pads zeros at end different than the length of scale - masking

In price mask (imask.js) I use this:
{
mask: Number,
signed: true,
scale: 3,
normalizeZeros: true,
padFractionalZeros: true,
...
}
This adds max 3 extra zeroes at the end, because the scale is 3. I want to be abble to add max 2, while the scale is 3.
The scale should be 3, but the added extra zeros at the end should be max 2. How to achieve this?
Example:
4 -> 4.00 // extra 2 zeros added
4.5 -> 4.50 // 1 zero added
4.56 -> 4.56 // nothing added
4.50 -> 4.50 // nothing added or removed
4.567 -> 4.567 // nothing added or removed

Related

Sum Up all Numbers before and after an operator Sign in a String user input

i am new to programming and starting up with kotlin . i've been stuck on this problem for a few days and i really need some assistance . i am trying to read a user input of Strings in a format like this 3 + 2 + 1, go through the String and wherever there is an operator , add up the numbers before and after the operator sign . so the above 3 + 2 + 1 should output 6.
Here's a snippet of my code
fun main() {
val userInput = readLine()!!.split(" ")
var sum = 0
for (i in 0 until userInput.size) {
if (userInput.get(i) == "+"){
sum += userInput.get(i-1).toInt() + userInput.get(i+1).toInt()
}
}
println(sum )
}
my code works until the point of adding up the numbers . it repeats the next number after the operator , so using the above example of 3 + 2 + 1 it outputs 8 thus 3 + 2 + 2 + 1. I'm so confused and don't know how to go about this .
Try not to increment the sum value each time, but rewrite the last number which was participated in sum. Just like that:
You have the case: 1 + 2 + 3 + 4
Split them
Now you have the array [1, +, 2, +, 3, +, 4]
Then you iterate this array, stuck with the first plus and sum the values.
Rewrite the second summed value with your sum.
Now you have new array [1, +, 3, +, 3, +, 4]
At the end of the loop, you will have this array [1, +, 3, +, 6, +, 10]
And your sum is the last element of the array
The logic of your code is that for each "+" encountered, it adds the sum of the numbers left and right of the "+" to the sum. For the example "1 + 2 + 3", here's what is happening:
Starting sum is 0.
At first "+", add 1 + 2 to sum, so now sum is 3.
At second "+", add 2 + 3 to sum, so now the sum is 3 + 5 = 8.
So you are adding all the middle numbers to the total twice, because they each appear next to two operators.
One way to do this is start with the first number as your sum. Then add only the number to the right of each "+", so numbers are only counted once.
fun main() {
val userInput = readLine()!!.split(" ")
var sum = userInput[0].toInt()
for (i in userInput.indices) {
if (userInput[i] == "+") {
sum += userInput[i + 1].toInt()
}
}
println(sum)
}
sum += userInput.get(i-1).toInt() + userInput.get(i+1).toInt()
This is only valid for the first iteration, so if the user puts 1 + 2 + 3.
So
userInput[0] is 1
userInput[1] is +...
the first time that line will be triggered sum will be 1 + 2, and that's quite fine, but the second time, in the second +, you will sum i-1 (which is 2 and was in the total sum already) and i+1, that will be 3, so you are doing 1+2+2+3.
You need to understand why this is happening and think of another way to implement it.
Check this is working for me
var sum = 0
readLine()?.split(" ")?.filter { it.toIntOrNull() != null }?.map { sum += it.toInt() }
println(sum)

how to create a function (number: Int), which accepts the parameter num as a number, then print numbers from 1 to num with the following conditions:

how to create a function (number: Int), which accepts the parameter num as a number,
then print numbers from 1 to num with the following conditions:
if the number can be divided by 3, then print a letter
if the number can be divided by 5, then print the word
if the numbers can be divided 3 & 5, then print a letterword
Example output:
letterword (5)
1
2
letter
4
word
Not sure what you mean by print 'the word'. Is there a specific word or letter?
For the conditions you can definitely use when, one of the Kotlin flow control operators and the Modulus(%) operator to check if number can be divided by the 3 or 5 or both
fun numberConditions(number : Int){
when{
//[number] can be divided by 3 and 5
number % 3 == 0 && number % 5 == 0 -> {
println("LetterWord")
}`enter code here`
//[number] can be divided by 3
number % 3 == 0 -> {
println("Letter")
}
//[number] can be divided by 5
number % 5 == 0 -> {
println("Word")
}
// [number] cannot be divided by 3 and 5
else -> {
println("number does not satisfy any of the conditions")
}
}
}

Divide list with the single number in tcl NS-2

i want to divide the whole list with one number.Lets say i take a variable $Content and i want to divide the following list with the 300 nodes.
so i take the command $Content/300
$Content= {1 2 3 4 5}{ 2 3 4 5 6} { 4 5 6 7 8 9}{3 4 6 8 9 0}
As a result output comes out {1 2 3 4 5}{ 2 3 4 5 6} { 4 5 6 7 8 9}{3 4 6 8 9 0}/300 with the parenthesis missing and invalid arguments.
Please tell me how we divide all list with the single number(300 nodes) because in curly brackets each number comes as an output of some arguments
Note that Tcl is a very whitespace-sensitive language, so you need a space between the close and open braces in your $Content declaration.
You can iterate over $Content, and for each sublist, iterate over the elements and divide by 300, collecting the results:
set Content {{1 2 3 4 5} { 2 3 4 5 6} { 4 5 6 7 8 9} {3 4 6 8 9 0}}
# note the spaces ......^............^..............^
set divisor 300
set newContent [list]
foreach sublist $Content {
set newSublist [list]
foreach elem $sublist {
lappend newSublist [expr {$elem * 1.0 / $divisor}]
}
lappend newContent $newSublist
}
puts $newContent
Output is
{0.0033333333333333335 0.006666666666666667 0.01 0.013333333333333334 0.016666666666666666} {0.006666666666666667 0.01 0.013333333333333334 0.016666666666666666 0.02} {0.013333333333333334 0.016666666666666666 0.02 0.023333333333333334 0.02666666666666667 0.03} {0.01 0.013333333333333334 0.02 0.02666666666666667 0.03 0.0}
If your Tcl version is 8.6 you can use the lmap command to shorten up the code:
set newContent [lmap sublist $Content {
lmap elem $sublist {expr {$elem * 1.0 / $divisor}}
}]
Note that I multiply by 1.0 in order to use float division and not integer division.

How to set dimensions of LineSeries

I have a line series that covers data from 1 to 20 along the X axis and 0 to 1 along the Y axis. My problem is that the x axis always starts at 0 and goes to the furthest value and the Y axis starts at 0 but only goes to the furthest value as well. How can I set these dimensions?
So for the Y axis this is what you want
plotModel.Axes.Add(new LinearAxis()
{
Title = "Y axis Name",
Position = AxisPosition.Left,
MinorStep = 0.1,
FilterMinValue = -1,
FilterMaxValue = 1
});
As for the X axis it seems to be more annoying handLabeling seems to be the only way.
plotModel.Axes.Add(new CategoryAxis()
{
Title = "X axis name",
Position = AxisPosition.Bottom,
Labels = {"1","2","3","4","5","6" } // ... upto 20
});
Then every item you add index the value of the Category label.
For example if I want an column of 64 on 3 in X axis then the code would look like this:
var item =new ColumnItem(62, 2); // index 2 being value "3"

Faster way to split a string and count characters using R?

I'm looking for a faster way to calculate GC content for DNA strings read in from a FASTA file. This boils down to taking a string and counting the number of times that the letter 'G' or 'C' appears. I also want to specify the range of characters to consider.
I have a working function that is fairly slow, and it's causing a bottleneck in my code. It looks like this:
##
## count the number of GCs in the characters between start and stop
##
gcCount <- function(line, st, sp){
chars = strsplit(as.character(line),"")[[1]]
numGC = 0
for(j in st:sp){
##nested ifs faster than an OR (|) construction
if(chars[[j]] == "g"){
numGC <- numGC + 1
}else if(chars[[j]] == "G"){
numGC <- numGC + 1
}else if(chars[[j]] == "c"){
numGC <- numGC + 1
}else if(chars[[j]] == "C"){
numGC <- numGC + 1
}
}
return(numGC)
}
Running Rprof gives me the following output:
> a = "GCCCAAAATTTTCCGGatttaagcagacataaattcgagg"
> Rprof(filename="Rprof.out")
> for(i in 1:500000){gcCount(a,1,40)};
> Rprof(NULL)
> summaryRprof(filename="Rprof.out")
self.time self.pct total.time total.pct
"gcCount" 77.36 76.8 100.74 100.0
"==" 18.30 18.2 18.30 18.2
"strsplit" 3.58 3.6 3.64 3.6
"+" 1.14 1.1 1.14 1.1
":" 0.30 0.3 0.30 0.3
"as.logical" 0.04 0.0 0.04 0.0
"as.character" 0.02 0.0 0.02 0.0
$by.total
total.time total.pct self.time self.pct
"gcCount" 100.74 100.0 77.36 76.8
"==" 18.30 18.2 18.30 18.2
"strsplit" 3.64 3.6 3.58 3.6
"+" 1.14 1.1 1.14 1.1
":" 0.30 0.3 0.30 0.3
"as.logical" 0.04 0.0 0.04 0.0
"as.character" 0.02 0.0 0.02 0.0
$sampling.time
[1] 100.74
Any advice for making this code faster?
Better to not split at all, just count the matches:
gcCount2 <- function(line, st, sp){
sum(gregexpr('[GCgc]', substr(line, st, sp))[[1]] > 0)
}
That's an order of magnitude faster.
A small C function that just iterates over the characters would be yet another order of magnitude faster.
A one liner:
table(strsplit(toupper(a), '')[[1]])
I don't know that it's any faster, but you might want to look at the R package seqinR - http://pbil.univ-lyon1.fr/software/seqinr/home.php?lang=eng. It is an excellent, general bioinformatics package with many methods for sequence analysis. It's in CRAN (which seems to be down as I write this).
GC content would be:
mysequence <- s2c("agtctggggggccccttttaagtagatagatagctagtcgta")
GC(mysequence) # 0.4761905
That's from a string, you can also read in a fasta file using "read.fasta()".
There's no need to use a loop here.
Try this:
gcCount <- function(line, st, sp){
chars = strsplit(as.character(line),"")[[1]][st:sp]
length(which(tolower(chars) == "g" | tolower(chars) == "c"))
}
Try this function from stringi package
> stri_count_fixed("GCCCAAAATTTTCCGG",c("G","C"))
[1] 3 5
or you can use regex version to count g and G
> stri_count_regex("GCCCAAAATTTTCCGGggcc",c("G|g|C|c"))
[1] 12
or you can use tolower function first and then stri_count
> stri_trans_tolower("GCCCAAAATTTTCCGGggcc")
[1] "gcccaaaattttccggggcc"
time performance
> microbenchmark(gcCount(x,1,40),gcCount2(x,1,40), stri_count_regex(x,c("[GgCc]")))
Unit: microseconds
expr min lq median uq max neval
gcCount(x, 1, 40) 109.568 112.42 113.771 116.473 146.492 100
gcCount2(x, 1, 40) 15.010 16.51 18.312 19.213 40.826 100
stri_count_regex(x, c("[GgCc]")) 15.610 16.51 18.912 20.112 61.239 100
another example for longer string. stri_dup replicates string n-times
> stri_dup("abc",3)
[1] "abcabcabc"
As you can see, for longer sequence stri_count is faster :)
> y <- stri_dup("GCCCAAAATTTTCCGGatttaagcagacataaattcgagg",100)
> microbenchmark(gcCount(y,1,40*100),gcCount2(y,1,40*100), stri_count_regex(y,c("[GgCc]")))
Unit: microseconds
expr min lq median uq max neval
gcCount(y, 1, 40 * 100) 10367.880 10597.5235 10744.4655 11655.685 12523.828 100
gcCount2(y, 1, 40 * 100) 360.225 369.5315 383.6400 399.100 438.274 100
stri_count_regex(y, c("[GgCc]")) 131.483 137.9370 151.8955 176.511 221.839 100
Thanks to all for this post,
To optimize a script in which I want to calculate GC content of 100M sequences of 200bp, I ended up testing different methods proposed here. Ken Williams' method performed best (2.5 hours), better than seqinr (3.6 hours). Using stringr str_count reduced to 1.5 hour.
In the end I coded it in C++ and called it using Rcpp, which cuts the computation time down to 10 minutes!
here is the C++ code:
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
float pGC_cpp(std::string s) {
int count = 0;
for (int i = 0; i < s.size(); i++)
if (s[i] == 'G') count++;
else if (s[i] == 'C') count++;
float pGC = (float)count / s.size();
pGC = pGC * 100;
return pGC;
}
Which I call from R typing:
sourceCpp("pGC_cpp.cpp")
pGC_cpp("ATGCCC")