Find Number of 0's at end of integer using POWER QUERY Power Bi - powerbi

I wanted to find out the number of 0's at end of integer.
Eg for 2020 it should count 1
for 2000 it should count 3
for 3010000 it should count 4
I have no idea to do it without counting all the zeros and not just the ending ones!
someone please help :)

Go to Power Query Editor and add a Custom Colum with this below code-
if Number.Mod([number],100000) = 0 then 5
else if Number.Mod([number],10000) = 0 then 4
else if Number.Mod([number],1000) = 0 then 3
else if Number.Mod([number],100) = 0 then 2
else if Number.Mod([number],10) = 0 then 1
else 0
Considered highst possibility of trailing 0 is 5. You can add more if/else case following the above logic if you predict more numbers of consecutive 0 at the end.
Here is sample output using above logic-

Take advantage of the fact, that text "00123" converted to number will be 2 characters shorter.
= let
TxtRev = Text.Reverse(Number.ToText([num]))&"1", /*convert to text and reverse, add 1 to handle num being 0*/
TxtNoZeroes = Number.ToText(Number.FromText(TxtRev)) /*convert to number to remove starting zeroes and then back to text*/
in
Text.Length(TxtRev)-Text.Length(TxtNoZeroes) /*compare length of original value with length without zeroes*/
This will work for any number of trailing zeroes (up to Int64 capacity of course, minus space for &"1"). Assuming that the column is of number type; if it's a text then just remove Number.ToText in TxtRev. If you have negative numbers or decimals, replace characters not being a digit after converting to text. For initial number being 0 it shows 1, but if it should show 0 just remove &"1".

You can do it as general string manipulation:
= Text.Length(Text.From([number])) - Text.Length(Text.TrimEnd(Text.From(number]), "0"))
We convert the column to string, strip of the zeroes, count take that away from the total length, giving you the amount of stripped zeroes.
Edit: I messed up my first answer, this one should in fact be correct

Related

Extracting numbers using Regex in Matlab

I would like to extract integers from strings from a cell array in Matlab. Each string contains 1 or 2 integers formatted as shown below. Each number can be one or two digits. I would like to convert each string to a 1x2 array. If there is only one number in the string, the second column should be -1. If there are two numbers then the first entry should be the first number, and the second entry should be the second number.
'[1, 2]'
'[3]'
'[10, 3]'
'[1, 12]'
'[11, 12]'
Thank you very much!
I have tried a few different methods that did not work out. I think that I need to use regex and am having difficulty finding the proper expression.
You can use str2num to convert well formatted chars (which you appear to have) to the correct arrays/scalars. Then simply pad from the end+1 element to the 2nd element (note this is nothing in the case there's already two elements) with the value -1.
This is most clearly done in a small loop, see the comments for details:
% Set up the input
c = { ...
'[1, 2]'
'[3]'
'[10, 3]'
'[1, 12]'
'[11, 12]'
};
n = cell(size(c)); % Initialise output
for ii = 1:numel(n) % Loop over chars in 'c'
n{ii} = str2num(c{ii}); % convert char to numeric array
n{ii}(end+1:2) = -1; % Extend (if needed) to 2 elements = -1
end
% (Optional) Convert from a cell to an Nx2 array
n = cell2mat(n);
If you really wanted to use regex, you could replace the loop part with something similar:
n = regexp( c, '\d{1,2}', 'match' ); % Match between one and two digits
for ii = 1:numel(n)
n{ii} = str2double(n{ii}); % Convert cellstr of chars to arrays
n{ii}(end+1:2) = -1; % Pad to be at least 2 elements
end
But there are lots of ways to do this without touching regex, for example you could erase the square brackets, split on a comma, and pad with -1 according to whether or not there's a comma in each row. Wrap it all in a much harder to read (vs a loop) cellfun and ta-dah you get a one-liner:
n = cellfun( #(x) [str2double( strsplit( erase(x,{'[',']'}), ',' ) ), -1*ones(1,1-nnz(x==','))], c, 'uni', 0 );
I'd recommend one of the loops for ease of reading and debugging.

I'm trying to encrypt a message for my homework assignment

The gist of it is that every letter from a-z needs to be encrypted into a number.
For example a will turn to "1", b into "2" all the way to z="26". Then I have to guess the number of possible outcomes for every encryption. For example 25114 can be 6 different thing. It can be BEAN,BEAAD,YAAD,YAN,YKD,BEKD.
My question is "How do I do this" ?
I've tried using if but it keeps printing "1" as an output every time.
#include <iostream>
using namespace std;
int main()
{
int a1,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z;
cout<<"vnesi kod"<<endl;
cin>>a1;
if (a)
{
cout<<"1"<<endl;
}
else if (b)
{
cout<<"2"endl;
}
return 0;
}
Since this is a homework problem, I just give you some pseudo-code on how to solve this. You will still have to implement it yourself.
Let us assume you get a number as input existing out of n digits: a1a2a3 ... an
Since the alphabet contains 26 letters, we want to split this number into groups of 1 or 2 digits and if we have a group of two digits, you have to check if the number is smaller than 27. The quickest way to do this is to make use of a recursive function. It is not the cleanest, but the quickest. Let us assume the recursive function is called decode.
It is very easy to understand why a recursive function is needed. If we want to decode the number 25114. There are two paths we need to take, groups of 1 and groups of 2:
group of 1: translate the last digit 4 into "D", and decode the remaining number 2511
group of 2: check if the last two digits are smaller than 27, translate the last two digits 14 into N and decode the remaining number 251
In pseudo-code this looks like this:
# function decode
# input: the number n to decode
# a postfix string p representing the decoded part
function decode(n, p) {
# end condition: If the number is ZERO, I have decoded the full number
# only print and return
if (n == 0) { print p; return }
# group of 1: use integer division to extract the
# last digit as n%10 and
# remainder to decode is n/10
decode(n/10, concat(translate(n%10),p) )
# group of 2: use integer division to extract the
# last two digits as n%100 and
# remainder to decode is n/100
# This does not need to run if n < 10 or if n%100 > 26
if (n > 9 && n%100 <= 26) { decode(n/100, concat(translate(n%100),p) ) }
}
The function concat concatenates two strings: concat("AA","BB") returns "AABB"
The function translate(n) converts a number n into its corresponding alphabetic character. This can be written as sprintf("%c",64+n)
As is mentioned in the comments, this is not a very efficient method. This is because we do the same work over and over. If the input reads 25114, we will do the following steps in order:
step 1: translate(4), decode _2511_
step 1.1: translate(1), decode _251_
step 1.1.1: ...
step 1.2: translate(11), decode _25_
step 1.2.1: ...
step 2: translate(14), decode _251_
as you see, we have to decode 251 twice (in step 1.1 and step 2). This is very inefficient as we do everything more than ones.
To improve this, you can keep track of what you have done so far in a lookup table
Check out the ASCII table http://www.asciitable.com/ . I have had something like this similar for my homework as well. since 'a' = 97 and 'z' = 122, you could subtract the desired character from 96 to get the preferred value from the casted character.
For example:
int letterNum {(int)'a' - 97 + 1}; // 1
int letterNum {(int)'z' - 97 + 1}; // 26

how to change the value as 0 when we have more than 10 digit in informatica

I have scenario like below..
I have column that has value like 12345678900.5555 and -12345678900.2582, I need to change the values to 0 because these column having more than 10 digit before dot(.). could help for this scenario...
Item Values return Values
12345678900.5555 0
-12345678900.5555 0
123456789.00 123456789
1234567890.00 1234567890
You can use the below logic:
IIF(Item_value>9999999999 OR (Item_value<0 AND Item_value<-9999999999),0,Item_value)
Get the number divided in whole number and decimal number.
If the count of whole number is greater than 0 it should be replaced with zero.
V_whole= SUBSTR( number,1,INSTR( number,'.' ,-1,1 ) - 1 )
V_decimal=SUBSTR(number,INSTR(number,'.',1,1)+1)
Iff(count(V_whole)>,0,V_whole)

Can't understand the proof behind the use of this map<> method

Drazil is playing a math game with Varda.
Let's define for positive integer x as a product of factorials of its
digits. For example, f(135) = 1! * 3! * 5! = 720.
First, they choose a decimal number a consisting of n digits that
contains at least one digit larger than 1. This number may possibly
start with leading zeroes. Then they should find maximum positive
number x satisfying following two conditions:
x doesn't contain neither digit 0 nor digit 1.
= f(x) = f(a)
Help friends find such number.
Input The first line contains an integer n (1 ≤ n ≤ 15) — the number
of digits in a.
The second line contains n digits of a. There is at least one digit in
a that is larger than 1. Number a may possibly contain leading zeroes.
Output Output a maximum possible integer satisfying the conditions
above. There should be no zeroes and ones in this number decimal
representation.
Examples
input
4
1234
output
33222
input
3
555
output
555
Here is the solution,
#include <bits/stdc++.h>
#include <algorithm>
using namespace std;
int main()
{
map<char, string> mp;
mp['0'] = mp['1'] = "";
mp['2'] = "2";
mp['3'] = "3";
mp['4'] = "223";
mp['5'] = "5";
mp['6'] = "35";
mp['7'] = "7";
mp['8'] = "2227";
mp['9'] = "2337";
int n;
string str;
cin>>n>>str;
string res;
for(int i = 0; i < str.size(); ++i)
res += mp[str[i]];
sort(res.rbegin(), res.rend());
cout<<res;
return 0;
}
I'd like if someone explains the reason why were the digits transformed into other form of digits rather than just with some way to compute the number with..sadly brute force would give a TLE(Time limit exceeded) in this question cause of the 15 digit thing so that's a big number to brute force to,so I kindly hope that someone can explain the "proof" below, cause idk what theory says that these numbers can be transformed to those numbers for example 4 to 223 and stuff.
Thanks in advance.
Picture: What the proof says
The theory behind these transformations is the following (Ill use 4 as an example):
4! = 3! * 2! * 2!
A longer sequence of digits will always produce a larger number than a shorter sequence (at least for positive integers). Thus this code produces a longer sequence where possible. With the above example we get:
4! = 3! * 4
We can't reduce the 3! any further, since 3 is a prime. 4 on the other hand is simply 2²:
4 = 2² = 2! * 2!
Thus we have found the optimal replacement for 4 in the number-sequence as "322". This can be done for all numbers, but prime-numbers aren't factorisable and will thus always be the best replacement available for them self.
And thanks to the fact that we're using prime factorization we also know that we have the only (and longest possible) string of digits that can replace a certain digit.

Extract Digits From Matlab String

In Matlab, let's say that I have the following string:
mystring = 'sdfkdsgoeskjgk elkr jtk34s ;3k54352642 643l j3kf p35j535';
And I want to extract all the digits in it to a vector such that each digit is standing by its own, so the output should be like:
output = [3 4 3 5 4 3 5 2 6 4 2....]
I tried to do it using this code and regex:
mystring = 'sdfkdsgoeskjgk elkr jtk34s ;3k54352642 643l j3kf p35j535';
digits = regexp(mystring, '[0-9]');
disp(digits);
But it gives me some weird 4-combined digits instead of what I need.
By default, the output of regexp is in the index of the first character in each match which is why the numbers aren't the same as the digits in your string. You'll want to use the output of regexp to then index into the initial string to get the digits themselves
digits = mystring(regexp(mystring, '[0-9]'));
You will still need to convert these from characters to numbers so you can subtract off '0' to do this conversion
digits = mystring(regexp(mystring, '[0-9]')) - '0';
Alternately, you could specify the 'match' input to regexp to return the actual matching string itself. This will return a cell array which we can then convert to an array of numbers using str2double
digits = str2double(regexp(mystring, '[0-9]', 'match'))
I use transposing instead of any other existing function to convert a string into an array.
mystring = 'sdfkdsgoeskjgk elkr jtk34s ;3k54352642 643l j3kf p35j535';
digits = regexp(mystring, '[0-9]');
array = double(mystring(digits)')'-48; % array of doubles
disp(array);