create a list from the individual values of one number - list

I am creating a way to convert an Arabic Numeral into a Roman Numeral. If the Arabic numeral to be converted is 124 I would like to create a list List that contains the values 100, 20, and 4. So basically I need to somehow find the base 10 decomposition of 124, and create a list of the values. Another example: 1,891 = 1,000 + 800 + 90 + 1, so the list could look like this: `list = [1000, 800, 90, 1]. I hope this explanation isn't too obscure for you to understand, and thank you.

Something like this would work:
def Roman(input):
digits = [int(i) for i in list(str(input))]
powers = range(len(digits))
powers.reverse()
return [digit * 10 ** power for digit, power in zip(digits, powers)]

Related

Regular expression in Matlab to leave only specified items of an array

I have a list of channels:
channels = {'1LT1', '1LT2', '1LT3', '1LT4', '1LT5', '2LA1', '2LA2', '2LA3', '3LH1', '3LH5', '4LT1', '4LT2', '4LT3', '5LH1', '5LH2', '4LT10'}
I need to write an alogrithm to leave only distal channels. It means for each type of channel ('1LT', '2LA', '3LH', '4LT' and etc.) I need only channel with the highest last number. The best way is to return indexes of these channels. For example, for abovementioned list the results should be:
[5, 8, 10, 15, 16]
I think I can do it with regexp by splitting like that:
row_i = 1;
for ch_i=[1:length(channels)]
try
[n(row_i,:), ch_type(row_i,:)] = strsplit(channels{ch_i},'\d+[A-Z]', 'DelimiterType','RegularExpression');
row_i = row_i + 1;
catch
continue
end
end
But then I am really stuck. Can somebody give me some tips to create good algorithm?
I am thankful for any idea!
You can use regexp to break each string into a channel and number, create numeric labels for the channels using findgroups, convert the number string into an actual number with str2double, then splitapply to find the max for each group. Here's the code, although I can't test it right now so it may need some tweaks:
tokens = regexp(channels, '(\d+[A-Z]+)(\d+)', 'tokens');
tokens = vertcat(tokens{:});
[grps, channelID] = findgroups(tokens(:, 1));
nums = str2double(tokens(:, 2));
channelMax = splitapply(#max, nums, grps);
Using the channelID and channelMax values, you can then reconstruct the distal channel names and find their indices in the channel list using sprintf, strsplit, and ismember:
distal = strsplit(sprintf('%s%d\n', channelID, channelMax));
index = find(ismember(channels, distal));

# function that given an array A consisting of N integers, returns the sum of all two -digit numbers.

function that given an array A consisting of N integers, returns the sum of all two -digit numbers.
def solution(A):
# write your code in Python 2.7
sum = 0
for i in A:
if i in range(0,1000):
sum = sum+i
return sum
A = [47,1900,1,90,45]
why would i get 183 instead of 182,please assist
Running solution...
Compilation successful.
Example test: [1, 1000, 80, -91]
WRONG ANSWER (got 81 expected -11)
Example test: [47, 1900, 1, 90, 45]
WRONG ANSWER (got 183 expected 182)
Detected some errors.
I think that in the first case you are just considering positive numbers and single digit numbers, which is in turn the problem for the second case.
test 1) 1+80=81
test 2) 47+1+90+45=183

Need help making a list for my programming 101 class

im new to programming and my programming assignment wants me to Create a 'list' called my_numbers which contains the odd numbers between 0 and 100 using a for-loop and I am trying to figure out how I can make a list and add 2 to the number in front of it and keep repeating. here is my code so far in python 3.5, thanks in advance to anyone who answers.
my_numbers = [1]
for i in range (0,100):
my_numbers = my_numbers + [2]
So your solution is:
my_numbers = range(1, 100, 2)
1 - start of range, inclusive
100 - end of range, non-inclusive
2 - step
Or in a for loop:
a = []
for x in range(1, 100):
if x % 2 == 1:
a.append(x)
Or in plain words:
For every number between 1 and 100, if you divide that number with 2 and have a reminder of 1 then add that number to the list.

Mod of two large numbers in C++

I have a class named LargeNum, which stores large numbers by array such as digit[]. Because int is not large enough to store it.
The base is 10000, so number '9876 8764 7263' is stored like:
digit[4] = {9876, 8764, 7263};
(the base can be changed into 10 or 100, like digit[12] = {9,8,7,6,8,7,6,4,7,2,6,3})
The problem is that I want to overload operator %, so than I can get the remainder of two large numbers. Overloading operator *, - between large numbers is finished by dealing with every digit of the large number. But I really don't how to do so with %. Like:
{1234,7890,1234} % {4567,0023}
Can anyone help me?
The pseudocode should be:
while digits_source > digits_base {
while first_digit_source > first_digit_base {
source -= base << (digits_source - digits_base)
}
second_digit_source += first_digit_source * LargeNum.base
first_digit_source = 0
digits_source--
}
while (source >= base) {
source -= base
}
return source
This should take advantage of your "digits" of the large number.
Edit: For simplicity, I am assuming that a single digit of you array can contain (numerically speaking) two digits. If it cannot, then the code would become quite tricky because you cannot do second_digit_source += first_digit_source * LargeNum.base
Edit:
Regarding an example operation (base = 10000)
{65,0000,0099} % {32,0001}
As 65 is > 32, then proceed to do:
65 - 32 = 33
0 - 1 = -1
Then we have {33, -1, 99} % {32, 1}. Proceed again
33 - 32 = 1
-1 - 1 = -2
We have {1, -2, 99} % {32, 1}. Because 32 > 1, the we join the two first digits of the source and we have {1*1000 - 2, 99} % {32, 1}. Now we can go into the simple while, simply by doing the minus operation. The while does a full comparison of source >= base because we cannot afford to have negative digits. However, during the first part of the algorithm we can because we are guaranteeing that the combination of the two first digits will be positive.

AS3 regex split

I want to split a number using regex. I have a number like xyz (x and y are single digits, z can be a 2 or three digit number), for example 001 or 103 or 112. I want to split it into separate numbers. This can be done, if I'm not wrong by doing split("",3); This will split the number (saved as string, but I don't think it makes difference in this case) 103 in an array with values 1,0,3.
Since here it's easy,the fact is that the last number z may be a 2 or 3 digit number.
So I could have 1034, 0001, 1011 so on. And I have to split it respectively into [1,0,34] [0,0,01] [1,0,11]
How can I do that?
Thanks
Sergiu
var regex:RegExp = /(\d)(\d)(\d+)/;
var n:Number = 1234;
var res:Array = regex.exec(n.toString()) as Array;
trace(res.join("\n"); /** Traces:
*
* 1234
* 1
* 2
* 34
*
* The first 1234 is the whole matched string
* and the rest are the three (captured) groups.
*/
Found the solution, I was going the hard way...it was just possible to use substr to substract the charcaters I want and the put them in an array.