How to iterate digits of integer? for example sum of digits here, it works, but is any way to right way?
int sumOfDigits(int num) {
int sum = 0;
String numtostr = num.toString();
for (var i = 0; i < numtostr.length; i++) {
sum = sum + int.parse(numtostr[i]);
}
return sum;
}
If you're looking for a shorter way to do this, you can combine split, map and reduce
int sum = num.split('').map((e) => int.parse(e)).reduce((t, e) => t + e);
You can even do this:
int sum = num.split('').map(int.parse).reduce((t, e) => t + e);
Thank you #julemand101
It's fairly inefficient to create a string, then split the string, and parse the individual digits back to integers.
How about something like:
Iterable<int> digitsOf(int number) sync* {
do {
yield = number.remainder(10);
number ~/= 10;
} while (number != 0);
}
This iterates the digits of the (non-negative) number in base 10, from least significant to most significant, without allocating any strings along the way.
If you want the digits in the reverse order, you can either create a list from the iterable above and reverse it, or use a different approach:
Iterable<int> digitsHighToLow(int number) sync* {
var base = 1;
while (base * 10 < number) {
base = base * 10;
}
do {
var digit = number ~/ base;
yield digit;
number = (number - digit * base) * 10;
} while (number != 0);
}
(again, only works on non-negative numbers, you'll have to figure out what you want for negative numbers, either throw, or try negating the number, it's the same digits after all, or something else).
Related
I am using fold on an array which hasn't been assign to a variable and want to check whether the element is the last value. With a conventional for loop I can do this:
List<int> ints = [1, 2, 3];
int sum = 0;
for (int num in ints]) {
if (num != ints.last) {
sum = sum + num;
}
}
print(sum);
Is it possible to do this with fold instead?
int foldSum = [1, 2, 3].fold(0, (int prev, element) => prev + element);
print(foldSum);
I can't find any way of check when fold is at the last value. Note: this is a simplified example of my problem and the reason the list isn't assigned to a variable (allowing me to use .last) is because it is the result of a call to .map().
For completeness, below is the actual code (which won't obviously won't be runnable in isolation but will help illustrate my problem) I am trying to convert to use .map and .fold:
String get fieldsToSqlInsert {
String val = "";
for (Column column in columns) {
if (data.containsKey(column.name)) {
val = '$val "${data[column.name]}"';
} else {
val = "$val NULL";
}
if (column != columns.last) {
val = "$val,";
}
}
return val;
}
But it doesn't work because I don't know how to check when fold is at the final element:
String get fieldsToSqlInsert => columns
.map((column) =>
data.containsKey(column.name) ? data[column.name] : "NULL")
.fold("", (val, column) => column != columns.last ? "$val," : val);
If you simply want to exclude the last element from further calculation, you can just use take to do so:
String get fieldsToSqlInsert => columns.take(columns.length - 1)...
a = (any random number)
Is there a way could I get the third number (12345) without converting it into a string?
If not, what would be a good way to get it by converting it into a string?
You can use the sub function
function getDigit(value,digitPlace)
return tonumber(tostring(value):sub(digitPlace,digitPlace))
end
This will get the third digit of a as a number:
a = 12345
print(getDigit(a,3))
You can get that using simple maths.
function getDigitInt(value, digit)
-- get rid of the sign
value = math.abs(value)
-- how many digits does the number have?
local numDigits = math.floor(math.log(value, 10)) + 1
-- does the requested digit exist?
if digit > numDigits or digit < 1 then
print("digit does not exist")
return
end
-- return the requested digit
return math.floor(value / 10^(numDigits - digit)) % 10
end
-- test
for i = 0, 8 do print(getDigitInt(1234567, i)) end
Add more error handling as needed. Also this can only handle integers of course. But I'm sure you will find out how to apply this idea to decimals as well.
You can convert the number into array and find the any place easily like blow
public int GetDigitsPlace(int number, int digitPlace) {
string t = number.ToString();
int[] nArr = new int[t.Length];
for(int i = 0; i < nArr.Length; i++) {
nArr[i] = int.Parse(t[i]);
}
return nArr[digitPlace];
}
Is there any way to generate random numbers without duplication?
For instance I want to generate 50 random numbers from 1 to 100 no duplication, any way to do this or do I have to check every time incoming number is already created or not?
you can use shuffle as following code.
import 'dart:math';
var list = new List<int>.generate(10, (int index) => index); // [0, 1, 4]
list.shuffle();
print(list);
You can use Set. Each object can occur only once when using it. Just try this:
Set<int> setOfInts = Set();
while (setOfInts.length < 50) {
setOfInts.add(Random().nextInt(range) + 1);
}
You can read the documentation here: Set Doc
Here is an alternative that avoids creating an array of all the possible values, and avoids repeatedly looping until no collision occurs. It may be useful when there is a large range to select from.
import 'dart:math';
class RandomList {
static final _random = new Random();
static List<int> uniqueSample({int limit, int n}) {
final List<int> sortedResult = [];
final List<int> result = [];
for (int i = 0; i < n; i++) {
int rn = _random.nextInt(limit - i); // We select from a smaller list of available numbers each time
// Increment the number so that it picks from the remaining list of available numbers
int j = 0;
for (; j < sortedResult.length && sortedResult[j] <= rn; j++) rn++;
sortedResult.insert(j, rn);
result.add(rn);
}
return result;
}
}
I haven't tested it exhaustively but it seems to work.
local function fShallowCopy(tData)
local tOutput = {}
for k,v in ipairs(tData) do
tOutput[k] = v
end
return tOutput
end
local function fLexTblSort(tA,tB) --sorter for tables
for i=1,#tA do
if tA[i]~=tB[i] then
return tA[i]<tB[i]
end
end
return false
end
function fBWT(tData)
--setup--
local iSize = #tData
local tSolution = {}
local tSolved = {}
--key table--
for n=1,iSize do
tData[iSize] = fRemove(tData,1)
tSolution[n] = fShallowCopy(tData)
end
table.sort(tSolution,fLexTblSort)
--encode output--
for i=1,iSize do
tSolved[i] = tSolution[i][iSize]
end
--finalize--
for i=1,iSize do
if fIsEqual(tSolution[i],tData) then
return i,tSolved
end
end
return false
end
Above is my current code for achieving BWT encoding in Lua. The issue is because of the size of the tables and lengths of loops it takes a long time to run. For a 1000 character input the average encoding time is about 1.15 seconds. Does anyone have suggestions for making a faster BWT encoding function?
the biggest slowdowns appear to be in fLexTblSort and fShallowCopy. I have included both above the BWT function as well.
If I see right, your algorithm has complexity O(n^2 log n), if the sort is quicksort. The comparator function fLexTblSort takes O(n) itself for each pair of values you compare.
As I checked with my implementation from few years back, I see possible space to improve. You create all the possible rotations of the tData, which takes also a lot of time. I used only single data block and I stored only starting positions of particular rotations. You also use a lot of loops which can shrink into less.
Mine implementation was in C, but the concept can be used also in Lua. The idea in some hybrid pseudocode between your Lua and C.
function fBWT(tData)
local n = #tData
local tSolution = {}
for(i = 0; i < n; i++)
tSolution[i] = i;
--table.sort(tSolution, fLexTblSort)
quicksort(tData, n, tSolution, 0, n)
for(i = 0; i < n; i++){
tSolved[i] = tData[(tSolution[i]+n-1)%n];
if( tSolution[i] == 0 )
I = i;
}
return I, tSolved
end
You will also need your own sort function, because the standard does not offer enough flexibility for this magic. Quicksort is a good idea (you might avoid some of the arguments, but I pasted just the C version I was using):
void swap(int array[], int left, int right){
int tmp = array[right];
array[right] = array[left];
array[left] = tmp;
}
void quicksort(uint8_t data[], int length, int array[], int left, int right){
if(left < right){
int boundary = left;
for(int i = left + 1; i < right; i++){
if( offset_compare(data, length, array, i, left) < 0 ){
swap(array, i, ++boundary);
}
}
swap(array, left, boundary);
quicksort(data, length, array, left, boundary);
quicksort(data, length, array, boundary + 1, right);
}
}
The last step is your own comparator function (similar to your original, but working on the rotations, again in C):
/**
* compare one string (fixed length) with different rotations.
*/
int offset_compare(uint8_t *data, int length, int *array, int first, int second){
int res;
for(int i = 0; i < length; i++){
res = data[(array[first]+i)%length] - data[(array[second]+i)%length];
if( res != 0 ){
return res;
}
}
return 0;
}
This is the basic idea I came up with few years ago and which worked for me. Let me know if there is something not clear or some mistake.
So I have a run method which summates the weights of the edges in the artificial neural network with the threshold values of the input nodes.
Sort of like this:
Now my test perceptron should produce a summation of -3, but I am getting a value of 1176!!! What is going on here?
Here is the code that I have written for my run() method, constructor, and my main method.
Constructor:
public class Perceptron {
//We want to create a variable which will represent the number of weighted edges
//in the 2-dimensional array.
protected int num_weighted_Edges;
//Inside this class we want to create a data field which is a
//2-D array of WeightedEdges. Since the weightedEdges will be in
//double data type, we will create a double type 2-dimensional
//array.
protected WeightedEdge[][] weightedEdges;
protected int[] weights;
//We set a double field named eta equal to 0.05.
protected double eta = 0.05;
//We initialize a constructor which only takes a parameter int n.
public Perceptron(int n){
//We want to create a new graph which will have n + 1 vertices
//, where we also want vertex 0 to act like the output node
//as in a neural network.
this.num_weighted_Edges = n;
weights = new int[num_weighted_Edges];
//First we need to verify that n is a positive real number
if (num_weighted_Edges < 0){
throw new RuntimeException("You cannot have a perceptron of negative value");
}
else {
//Test code for testing if this code works.
System.out.println("A perceptron of " + num_weighted_Edges + " input nodes, and 1 output node was created");
}
//Now we create a graph object with "n" number of vertices.
weightedEdges = new WeightedEdge[num_weighted_Edges + 1][num_weighted_Edges + 1];
//Create a for loop that will iterate the weightedEdges array.
//We want to create the weighted edges from vertex 1 and not vertex 0
//since vertex 0 will be the output node, so we set i = 1.
for (int i = 1; i < weightedEdges.length; i++){
for (int j = 0; j < weightedEdges[i].length; j++){
//This will create a weighted edge in between [1][0]...[2][0]...[3][0]
//The weighted edge will have a random value between -1 and 1 assigned to it.
weightedEdges[i][0] = new WeightedEdge(i, j, 1);
}
}
}
This is my run() method:
//This method will take the input nodes, do a quick verification check on it and
//sum up the weights using the simple threshold function described in class to return
//either a 1 or -1. 1 meaning fire, and -1 not firing.
public int run(int[] weights){
//So this method will act like the summation function. It will take the int parameters
//you put into the parameter field and multiply it times the input nodes in the
//weighted edge 2 d array.
//Setup a summation counter.
int sum = 0;
if (weights.length != num_weighted_Edges){
throw new RuntimeException("Array coming in has to equal the number of input nodes");
}
else {
//We iterate the weights array and use the sum counter to sum up weights.
for (int i = 0; i < weights.length; i++){
//Create a nested for loop which will iterate over the input nodes
for ( int j = 1; j < weightedEdges.length; j++){
for (int k = 0; k < weightedEdges[j].length; k++){
//This takes the weights and multiplies it times the value in the
//input nodes. The sum should equal greater than 0 or less than 0.
sum += (int) ((weightedEdges[j][0].getWeight()) * i);
//Here the plus equals sign takes the product of (weightedEdges[j][0] * i) and
//then adds it to the previous value.
}
}
}
}
System.out.println(sum);
//If the sum is greater than 0, we fire the neuron by returning 1.
if (sum > 0){
//System.out.println(1); test code
return 1;
}
//Else we don't fire and return -1.
else {
//System.out.println(-1); test code
return -1;
}
}
This is my main method:
//Main method which will stimulate the artificial neuron (perceptron, which is the
//simplest type of neuron in an artificial network).
public static void main(String[] args){
//Create a test perceptron with a user defined set number of nodes.
Perceptron perceptron = new Perceptron(7);
//Create a weight object that creates an edge between vertices 1 and 2
//with a weight of 1.5
WeightedEdge weight = new WeightedEdge(1, 2, 1.5);
//These methods work fine.
weight.getStart();
weight.getEnd();
weight.setWeight(2.0);
//Test to see if the run class works. (Previously was giving a null pointer, but
//fixed now)
int[] test_weight_Array = {-1, -1, -1, -1, -1, 1, 1};
//Tested and works to return output of 1 or -1. Also catches exceptions.
perceptron.run(test_weight_Array);
//Testing a 2-d array to see if the train method works.
int[][] test_train_Array = {{1}, {-1}, {1}, {1}, {1}, {1}, {1}, {1}};
//Works and catches exceptions.
perceptron.train(test_train_Array);
}
}
I think you should change
sum += (int) ((weightedEdges[j][0].getWeight()) * i);
to
sum += (int) ((weightedEdges[j][k].getWeight()) * i);