![]() |
[QUOTE=science_man_88;239308]I couldn't get the digits placed properly as i didn't have a formula I found one that worked except a shift I made a shift of all but answer[1] then I shifted answer[1] separately, and yes you would as it determines what stays in position 1.[/QUOTE]
If you look at my code, the formula is embarrassingly simple. (I could hardly believe it when I saw that pretty much everything cancels...) |
[CODE]pascals_triangle(N)= {
my(row=[],prevrow=[]) for(x=1,N, if(x>5,break(1)); row=eval(Vec(Str(11^(x-1)))); print(row)); prevrow=row; for(y=6,N, for(p=2,#prevrow, row[p]=prevrow[p-1]+prevrow[p]); row=concat(row,1); prevrow=row; print(row); ); }[/CODE] good enough code ? I added my() in just to satisfy you even though i see no point of it. |
[CODE]RLE(string)={
my(number=1,string=eval(Vec(Str(string))),Letter=string[1]); for(x=2,#string, if(string[x]==Letter, number+=1, print1("("number")"Letter); Letter=string[x]; number=0) ); }[/CODE] This is my best try at RLE I have a book that has it and LZW. I tried it with RLE(wwwbbbbwwww) it failed to code the last w's. |
[QUOTE=science_man_88;239343]good enough code ?[/QUOTE]
I don't understand where the constants 5 and 6 come from. [QUOTE=science_man_88;239343]I added my() in just to satisfy you even though i see no point of it.[/QUOTE] It is *perfectly* acceptable to not understand why my() is used. In fact, I would generally avoid telling people I'm teaching why they use my() at all -- just have them use it whenever they define their own variables. |
[QUOTE=CRGreathouse;239374]I don't understand where the constants 5 and 6 come from.
It is *perfectly* acceptable to not understand why my() is used. In fact, I would generally avoid telling people I'm teaching why they use my() at all -- just have them use it whenever they define their own variables.[/QUOTE] I did the 5 and 6 because up until row 5 of the triangle they easily conform to the digits of 11^row number. and I used a slower form of adding them together from there until N if it's above 5. |
[QUOTE=science_man_88;239348][CODE]RLE(string)={
my(number=1,string=eval(Vec(Str(string))),Letter=string[1]); for(x=2,#string, if(string[x]==Letter, number+=1, print1("("number")"Letter); Letter=string[x]; number=0) ); }[/CODE] This is my best try at RLE I have a book that has it and LZW. I tried it with RLE(wwwbbbbwwww) it failed to code the last w's.[/QUOTE] Very nice code formatting!:smile: First, you should take input as a string: trust me, this is the right way. So call it with RLE("wwwbbbbwwww") and just call string=Vec(string). The code should (presumably, didn't read the task description) *either* add 1 *or* print the current total for the last letter, reset the letter to the current letter, and reset the count. |
[QUOTE=CRGreathouse;239377]Very nice code formatting!:smile:
First, you should take input as a string: trust me, this is the right way. So call it with RLE("wwwbbbbwwww") and just call string=Vec(string). The code should (presumably, didn't read the task description) *either* add 1 *or* print the current total for the last letter, reset the letter to the current letter, and reset the count.[/QUOTE] yeah I figured that was coming... yeah that's what the if is trying to do and it works until it hits the second set of w's but then fails. this may be something to keep my mind busy while my mom checks through emergency to fix a portacast that seems to be coming out! and a lower body temperature. |
[QUOTE=science_man_88;239376]I did the 5 and 6 because up until row 5 of the triangle they easily conform to the digits of 11^row number. and I used a slower form of adding them together from there until N if it's above 5.[/QUOTE]
I just noticed that you formatting is deceptive. I've highlighted the two main loops in red and blue: [code]pascals_triangle(N)= { my(row=[],prevrow=[]) [COLOR="Red"]for(x=1,N, if(x>5,break(1)); row=eval(Vec(Str(11^(x-1)))); print(row));[/COLOR] prevrow=row; [COLOR="Blue"]for(y=6,N, for(p=2,#prevrow, row[p]=prevrow[p-1]+prevrow[p]); row=concat(row,1); prevrow=row; print(row); );[/COLOR] }[/code] I suspect that the indentation reflects your intent and the parentheses need to be moved to match. |
[QUOTE=science_man_88;239378]yeah that's what the if is trying to do and it works until it hits the second set of w's but then fails.[/QUOTE]
Actually... :blush: it looks like that part of your code is OK. |
[QUOTE=CRGreathouse;239379]I just noticed that you formatting is deceptive. I've highlighted the two main loops in red and blue:
[code]pascals_triangle(N)= { my(row=[],prevrow=[]) [COLOR="Red"]for(x=1,N, if(x>5,break(1)); row=eval(Vec(Str(11^(x-1)))); print(row));[/COLOR] prevrow=row; [COLOR="Blue"]for(y=6,N, for(p=2,#prevrow, row[p]=prevrow[p-1]+prevrow[p]); row=concat(row,1); prevrow=row; print(row); );[/COLOR] }[/code] I suspect that the indentation reflects your intent and the parentheses need to be moved to match.[/QUOTE] copying your format I found another missed semi colon |
[QUOTE=CRGreathouse;239380]Actually... :blush: it looks like that part of your code is OK.[/QUOTE]
[CODE]RLE(string)={ my(number=1,string=eval(Vec(Str(string))),Letter=string[1]); for(x=2,#string, if(string[x]==Letter, number+=1, print1("("number")"Letter); Letter=string[x]; number=0) ); }[/CODE] RLE() takes the [COLOR="Red"]string[/COLOR] puts the first letter in[COLOR="Lime"] Letter[/COLOR] and puts an array of characters in [COLOR="Red"]string[/COLOR] and start [COLOR="DarkOrange"]number[/COLOR] off at 1 to represent the first letter in the count. if then goes from string[2] to string[#string] checking if the next letter matches [COLOR="Lime"]Letter[/COLOR] if it matches number increases.Otherwise it prints ([COLOR="rgb(255, 140, 0)"]number[/COLOR])[COLOR="Lime"]Letter[/COLOR] then sets [COLOR="Lime"]Letter [/COLOR]to the letter that doesn't match. Then it resets [COLOR="rgb(255, 140, 0)"]number[/COLOR] to be 0 which actually should be 1. fixed it I forgot a final print after the loop. |
| All times are UTC. The time now is 23:10. |
Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2021, Jelsoft Enterprises Ltd.