Triangle Sum through down with NON-PRIME numbers with Swift 4 - swift
I know this question is asked before for different programming languages, and I tried to implement this with Swift 4 but once I submit my answer, I've been told that my answer was wrong so here is the task;
You will have a TRIANGLE input from a file and you need to find the maximum sum of the numbers according to given rules below;
You will start from the top and move downwards to an adjacent number as in below.
You are only allowed to walk downwards and diagonally.
You can only walk over NON PRIME NUMBERS.
According to above rules the maximum sum of the numbers from top to bottom in below example is 24.
var sampleString = """
1
8 4
2 6 9
8 5 9 3
As you can see this has several paths that fits the rule of NOT PRIME NUMBERS; 1>8>6>9, 1>4>6>9, 1>4>9>9
1 + 8 + 6 + 9 = 24. As you see 1, 8, 6, 9 are all NOT PRIME NUMBERS and walking over these yields the maximum sum.
assignment string:
var assignmentString = """
215
193 124
117 237 442
218 935 347 235
320 804 522 417 345
229 601 723 835 133 124
248 202 277 433 207 263 257
359 464 504 528 516 716 871 182
461 441 426 656 863 560 380 171 923
381 348 573 533 447 632 387 176 975 449
223 711 445 645 245 543 931 532 937 541 444
330 131 333 928 377 733 017 778 839 168 197 197
131 171 522 137 217 224 291 413 528 520 227 229 928
223 626 034 683 839 53 627 310 713 999 629 817 410 121
924 622 911 233 325 139 721 218 253 223 107 233 230 124 233"""
my code:
func maxSumForTriangle(triangleString: String) {
var temporaryIndex = 0
var earlierIndex = 0
var greatSum = 0
var temporaryMaxInLine = 0
let values = triangleString.components(separatedBy: .newlines).map {
$0.components(separatedBy: .whitespaces).compactMap(Int.init)
}
print(values)
print(values.count)
for line in values {
if line.count == 1 {
greatSum += line[0]
earlierIndex = line.count - 1
} else {
for number in line.enumerated() {
if number.offset == earlierIndex || number.offset == earlierIndex + 1 {
//Check the number if its prime or not with the isPrime function we defined
if !isPrime(number.element) {
if number.element > temporaryMaxInLine {
temporaryMaxInLine = number.element
temporaryIndex = number.offset
}
}
}
}
earlierIndex = temporaryIndex
greatSum += temporaryMaxInLine
temporaryMaxInLine = 0
}
}
print(greatSum)
}
Which results in 7619 but then I realized where my problem is; I don't check for every possible path, I just check for the highest non prime number at each line and continue summing with it.
So I need to find a different approach for this problem so that my function can check every possible scenarios and return with the highest sum
I could not figure it out yet, should I implement a different function where it calls itself again so that it can check for all the possible paths?
Sorry for long question but I also wanted to show my old implementation.
This is a typical problem which can be solved with “dynamic programming”: The idea
is to compute the maximal possible sum for every starting
point in the pyramid.
And that becomes simple if we start at the bottom row and work upwards:
We only have to add to each entry the larger of its two lower neighbors.
At the end, the top entry is the desired maximal sum.
Taking the additional condition about non-primes into account, this
can be implemented as
var values = triangleString.components(separatedBy: .newlines).map {
$0.components(separatedBy: .whitespaces).compactMap(Int.init)
}
for row in values.indices.reversed() {
for col in values[row].indices {
if isPrime(values[row][col]) {
values[row][col] = Int.min
} else if row + 1 < values.endIndex {
values[row][col] += max(values[row+1][col], values[row+1][col+1])
}
}
}
print(values[0][0])
Related
In KDB, how do I sum the previous 3 numbers in a list?
Say I have a list of numbers: j: (til 40)*9 0 9 18 27 36 45 54 63 72 81 90 99 108 117 126 135 144 153 162 171 180 189 198 207 216 225 234 243 252 261 270 279 288 297 306 315 324 333 342 351 What's the most elegant way to get the sum of the previous 3 (or n) numbers in the list? (Ideally considering large RAM constrained lists).
Does this work? q)3 msum j 0 9 27 54 81 108 135 162 189 216 243 270 297 324 351 378 405 432 459 486 513 ..
function returning vector in matlab
I have a function returning a vector in MatLab, the problem is that function should return a unsigned integer value, I've debugged my code and I realize that the variable galois_value turns into a vector when I do a bitand and a bitxor operation. I've tried to do a typecast to turn the vector into a unsigned value but doesn't works. I've tried to force a cast, but doesn't works too. Below the function: function galois_value = galois_mul2( value ) hex = uint8(hex2dec('1B')); temp = typecast(value, 'int8'); temp = bitshift(temp,-7); temp = bitand(typecast(temp,'uint8'),hex); galois_value = bitxor(bitshift(value,1),uint16(temp)); end The correct output of this function should be (this output comes from a C code that is working) : 96 210 97 224 119 194 192 156 196 195 102 10 10 117 235 213 49 57 235 79 172 5 23 62 111 188 223 128 113 133 128 102 30 238 226 31 The output I get with the MatLab function: 96 96 210 210 353 378 224 224 375 364 194 194 192 192 156 156 196 196 451 472 102 102 10 10 10 10 373 366 491 496 469 462 305 298 313 290 491 496 335 340 172 172 261 286 279 268 62 62 367 372 188 188 479 452 128 128 369 362 389 414 128 128 102 102 30 30 238 238 226 226 287 260 The code to display and debug the function: %Key key = {'00','01','02','03','04','05','06','07','08','09','0a','0b','0c','0d','0e','0f'}; for n = 1 : 16 keyU(n)=uint16(hex2dec(key(n))); end %State state = {'00','11','22','33','44','55','66','77','88','99','aa','bb','cc','dd','ee','ff'}; for n = 1 : 16 stateU(n)=uint16(hex2dec(state(n))); end %Sbox sbox = {'63','7c','77','7b','f2','6b','6f','c5','30','01','67','2b','fe','d7','ab','76','ca','82','c9','7d','fa','59','47','f0','ad','d4','a2','af','9c','a4','72','c0','b7','fd','93','26','36','3f','f7','cc','34','a5','e5','f1','71','d8','31','15','04','c7','23','c3','18','96','05','9a','07','12','80','e2','eb','27','b2','75','09','83','2c','1a','1b','6e','5a','a0','52','3b','d6','b3','29','e3','2f','84','53','d1','00','ed','20','fc','b1','5b','6a','cb','be','39','4a','4c','58','cf','d0','ef','aa','fb','43','4d','33','85','45','f9','02','7f','50','3c','9f','a8','51','a3','40','8f','92','9d','38','f5','bc','b6','da','21','10','ff','f3','d2','cd','0c','13','ec','5f','97','44','17','c4','a7','7e','3d','64','5d','19','73','60','81','4f','dc','22','2a','90','88','46','ee','b8','14','de','5e','0b','db','e0','32','3a','0a','49','06','24','5c','c2','d3','ac','62','91','95','e4','79','e7','c8','37','6d','8d','d5','4e','a9','6c','56','f4','ea','65','7a','ae','08','ba','78','25','2e','1c','a6','b4','c6','e8','dd','74','1f','4b','bd','8b','8a','70','3e','b5','66','48','03','f6','0e','61','35','57','b9','86','c1','1d','9e','e1','f8','98','11','69','d9','8e','94','9b','1e','87','e9','ce','55','28','df','8c','a1','89','0d','bf','e6','42','68','41','99','2d','0f','b0','54','bb','16'}; for n = 1 : 256 sboxU(n)=uint16(hex2dec(sbox(n))); end %Rcon rcon = {'01','02','04','08','10','20','40','80','1b','36'}; for n = 1 : 10 rconU(n)=uint16(hex2dec(rcon(n))); end %Main AES Data Loop for round = 1 : 10 %Add key + sbox for i = 1 : 16 stateU(i)= sboxU(bitxor(stateU(i),keyU(i))+1); end %Shift Rows buf1 = stateU(2); stateU(2) = stateU(6); stateU(6) = stateU(10); stateU(10) = stateU(14); stateU(14) = buf1; buf1 = stateU(3); buf2 = stateU(7); stateU(3) = stateU(11); stateU(7) = stateU(15); stateU(11) = buf1; stateU(15) = buf2; buf1 = stateU(16); stateU(16) = stateU(12); stateU(12) = stateU(8); stateU(8) = stateU(4); stateU(4) = buf1; %Process mixcolumn for all rounds but the last one if round < 10 for j = 0 : 3 %Compute the current index buf4 = (bitshift(j,2)); %buf1 aux1 = bitxor(stateU(buf4+1),stateU(buf4+2)); aux2 = bitxor(stateU(buf4+3),stateU(buf4+4)); buf1 = bitxor(aux1,aux2); %buf2 buf2 = stateU(buf4+1); %buf3 buf3 = bitxor(stateU(buf4+1),stateU(buf4+2)); buf3 = galois_mul2(buf3); disp(buf3); end end end
The problem is in line 3 of galois_mul2 function temp = typecast(value, 'int8'); since value has type uint16 output of typecast is two elements. for e.g. output of typecast(uint16(5), 'int8') is 5 0 If you change all types in lines 3,8,13,18 of main and lines 3,6 of galois_mul2 function to uint8 the problem will be solved.
How do I get rid of the subscript indices error in my code?
length = 31 ; while length < 0 length = input('enter a value greater than 0:') end pounds = 26905; elasticity = 45941267 ; width = 4.3 while width < 0 width = input('enter a value greater than 0:') end height = 1.2 while height < 0 height = input('enter a value greater than 0:'); end I = (width*height^3)/12; a = linspace(1,200)'; b = length - a ; if a >= 0 maximum = (-pounds*b(length.^2-(b.^2))^(3/2))/(9*sqrt(3)*elasticity*I*length)' elseif b >= 0 maximum = (-pounds*a(length.^2-(a.^2))^(3/2))/(9*sqrt(3)*elasticity*I*length)' end It happens at this line: maximum = (-pounds*b(length.^2-(b.^2))^(3/2))/(9*sqrt(3)*elasticity*I*length)' I need my code to come out with these numbers so i can print them in a table and be done with my code.
As #rayryeng stated, the problem you hay is that you are indexing into b. If you examine length^2 - (b.^2) you get: 61 120 177 232 285 336 385 432 477 520 561 600 637 672 705 736 765 792 817 840 861 880 897 912 925 936 945 952 957 960 961 960 957 952 945 936 925 912 897 880 861 840 817 792 765 736 705 672 637 600 561 520 477 432 385 336 285 232 177 120 61 0 -63 -128 -195 -264 -335 -408 -483 -560 ... snipped The fourth value is: 232 which is greater than b's length (200 in this case). That triggers the first error, but even if you were able to continue you'll eventually get to negative values (for an index! Another error there)
MATLAB accessing conditional values and performing operation in single column
Just started MATLAB 2 days ago and I can't figure out a non-loop method (since I read they were slow/inefficient and MATLAB has better alternatives) to perform a simple task. I have a matrix of 5 columns and 270 rows. What I want to do is: if the value of an element in column 5 of matrix goodM is below 90, I want to take that element and and subtract it from 90. So far I tried: test = goodM(:,5) <= 90; goodM(test) = 999; It changes all goodM values within column 1 not 5 into 999, in addition this method doesn't allow me to perform operations on the elements below 90 in column 5. Any elegant solution to doing this? edit:: goodM(:,5)(test) = 999; doesn't seem to work either so I have no idea to specify the target column.
I am assuming you are looking to operate on elements that have values below 90 as your text in the question reads, rather than 'below or equal to' as represented by '<=' as used in your code. So try this - ind = find(goodM(:,5) < 90) %// Find indices in column 5 that have values less than 90 goodM(ind,5) = 90 - goodM(ind,5) %// Operate on those elements using indices obtained from previous step
Try this code: b=90-a(a(:,5)<90,5); For example: a = 265 104 479 13 176 26 110 447 208 144 379 163 179 366 464 301 48 274 391 26 429 374 174 184 297 495 375 312 373 82 465 272 399 447 420 205 170 373 122 84 1 417 63 65 252 271 277 412 113 500 then, b=90-a(a(:,5)<90,5); b = 64 8 6
subtracting two matrices in matlab, the negative values in result are substituted by zero
I have two matrices in matlab, > IRwindow = > > **183** 171 150 125 137 138 167 184 173 152 105 114 141 167 185 148 113 105 115 141 186 183 147 112 105 > > ILwindow = > > **201** 170 165 177 203 181 174 167 169 189 154 150 156 168 181 187 175 158 131 144 173 186 183 167 141 I want to subtract these two matrices element-wise and get the result; for example for first element (183 - 201= -18 ) BUT the output for this element gives zero. the outcome result will be as below: > IRwindow - ILwindow ans = **0** 1 0 0 0 0 0 17 4 0 0 0 0 0 4 0 0 0 0 0 13 0 0 0 0 how could I keep the real results? without getting zero for negatives in my result-matrix
Run the following example code: %# Create random matrices X = randi(100, 5, 5); Y = randi(100, 5, 5); %# Convert to strictly non-negative format X = uint8(X); Y = uint8(Y); %# Perform subtractions A = X - Y; %# Convert to double format X = double(X); Y = double(Y); %# Perform subtraction B = X - Y; For a given sample run: A = 0 15 36 0 0 0 0 0 0 3 0 0 0 25 0 13 0 15 0 0 0 49 0 0 14 while: B = -8 15 36 -4 -65 0 -47 -45 -11 3 -18 -17 -11 25 -52 13 -53 15 -15 -1 -35 49 -47 -8 14 You will notice that all the negative numbers in A have been replaced by 0, while the negative numbers in B are displayed correctly. Stated simply: if you use a numerical format that is not able to store negative numbers, then Matlab truncates at 0. The solution is to convert to a format that is able to accomodate "real" numbers (or a close approximation thereof) such as double, or perhaps in your case one of the int formats may be more appropriate, such as int8, int16, int32 or int64.
Another option is to use single or double on the subtraction in one line as follows: ans=double(IRwindow-ILwindow)
I dont get the same problem as you: I have this code: IRwindow = [ 183 171 150 125 137 138 167 184 173 152 105 114 141 167 185 148 113 105 115 141 186 183 147 112 105] ILwindow = [ 201 170 165 177 203 181 174 167 169 189 154 150 156 168 181 187 175 158 131 144 173 186 183 167 141] IRwindow - ILwindow and i get this output: IRwindow = 183 171 150 125 137 138 167 184 173 152 105 114 141 167 185 148 113 105 115 141 186 183 147 112 105 ILwindow = 201 170 165 177 203 181 174 167 169 189 154 150 156 168 181 187 175 158 131 144 173 186 183 167 141 ans = -18 1 -15 -52 -66 -43 -7 17 4 -37 -49 -36 -15 -1 4 -39 -62 -53 -16 -3 13 -3 -36 -55 -36 Check that you are creating your matrices are being created properly (as doubles and not as unsigned integers).