tdxev Posted October 8, 2010 Report Posted October 8, 2010 Se da functia wordGenerator care genereaza o lista de cuvinte formata din caracterele primite ca argument ex. wordGenerator('abc'); Lungimea sirului de caractere e variabila.x = wordGenerator('abc'); // Lista creata este de lungime infinita x = 1 - a2 - b3 - c4 - aa5 - ab6 - ac7 - ba8 - bb9 - bc10 - ca11 - cb12 - cc13 - aaa14 - aab15 - aac16 - aba17 - abb18 - abc19 - aca20 - acb21 - acc22 - baa23 - bab24 - bac25 - bba26 - bbb27 - bbc28 - bca29 - bcb30 - bcc31 - caa32 - cab33 - cac34 - cba35 - cbb36 - cbc37 - cca38 - ccb39 - ccc40 - aaaa41 - aaab42 - aaac43 - aaba44 - aabb45 - aabc46 - aaca47 - aacb48 - aacc49 - abaa50 - abab51 - abac52 - abba53 - abbb54 - abbc55 - abca56 - abcb57 - abcc58 - acaa59 - acab60 - acac61 - acba62 - acbb63 - acbc64 - acca65 - accb66 - accc67 - baaa68 - baab69 - baac70 - baba71 - babb72 - babc73 - baca74 - bacb75 - bacc76 - bbaa77 - bbab78 - bbac79 - bbba80 - bbbb81 - bbbc82 - bbca83 - bbcb84 - bbcc85 - bcaa86 - bcab87 - bcac88 - bcba89 - bcbb90 - bcbc91 - bcca92 - bccb93 - bccc94 - caaa95 - caab96 - caac97 - caba98 - cabb99 - cabc100 - caca101 - cacb102 - cacc103 - cbaa104 - cbab105 - cbac106 - cbba107 - cbbb108 - cbbc109 - cbca110 - cbcb111 - cbcc112 - ccaa113 - ccab114 - ccac115 - ccba116 - ccbb117 - ccbc118 - ccca119 - cccb120 - cccc121 - aaaaa122 - ..........9999999999999999999999999999999999...Se cere crearea unei functi (metode) care sa returneze rezultatul de la linia 48884560, pentru sirul de caractere "abc", fara a genera/parcurge intreaga lista de cuvinte;Se poate folosi ori ce limbaj de programare doriti.Mai pe scurt vreau rezultatul de la linia : 48884560; Quote
cmiN Posted October 9, 2010 Report Posted October 9, 2010 Enter charset: abcLine: 48884560bccabbbaacbbbcbaSub o secunda -->Python:#! /usr/bin/env python# 09.10.2010 <> 09.10.2010 | cmiN# wordGenerator 4 tdxev @ rstcenter.comdef main(): charset = raw_input("Enter charset: ") line = int(raw_input("Line: ")) base = len(charset) result = 0 exponent = 0 genstr = "" while result < line: exponent += 1 result += base ** exponent result -= base ** exponent for e in xrange(exponent - 1, -1, -1): pos = -1 while result < line: result += base ** e pos += 1 result -= base ** e genstr += charset[pos] print genstrif __name__ == "__main__": main()Sigur sunt metode recursive sau de backtracking sau plm ce se foloseste mult mai rapide . Oricum in py se misca aproape instantaneu, deci in C ... Quote
tdxev Posted October 9, 2010 Author Report Posted October 9, 2010 aceasta este functia creata de mine , arata ca naiba dar merge function wordGeneratorBackDoor($charset,$nr){ $w=strlen($charset); if ($nr<=$w){return substr($charset,$nr-1,1);} $CLb=1; $CLsum=0; $CLsumi=0; $CLnr=0; while ($nr>$CLsum){ $CLnr++; $CLsumi += $CLb; $CLb = pow($w,$CLnr); $CLsum += $CLb; } $CLsumi--; $CLp=$nr-$CLsumi; $CLcur=$CLb*$w; $thex=''; for ($i=$CLnr; $i>0; $i--){ $CLcur=$CLcur/$w; $m=$CLcur/$w; if ($m==1){ $rest=$nr%$w; if ($rest==0) { $thex.=substr($charset,$w-1,1); } else { $thex.=substr($charset,$rest-1,1); } } if ($m!=1) for ($k=1; $k<=$w; $k++){ if ((($m*$k)>=$CLp) && ($CLp < ($m*($k+1)))){ $thex.=substr($charset,$k-1,1); if ($k>1) {$CLp=$CLp-(($k-1)*($m));} $k=$w+1; } } } return $thex;}echo wordGeneratorBackDoor('abc',48884560); Quote
kNigHt Posted October 10, 2010 Report Posted October 10, 2010 #include <iostream>#include <math.h>using namespace std;unsigned long x;unsigned long count = 0, pointer = 1;int a[500];int get_nr(int start, int div, int point) { if(start + div >= x) return 1; if(start + div*2 >= x) return 2; if(start + div*3 >= x) return 3;}void gimme(int start, int point) { if(point > 0) { int div = pow(3, point-1); a[pointer-point] = get_nr(start, div, point); gimme(start + div*(a[pointer-point]-1) , point-1); } }int main() { cout << "Introdu pozitia: "; cin >> x; while(count <= x) { count += pow(3, pointer); pointer++; } pointer--; int start_pos = count - pow(3, pointer); gimme(start_pos, pointer); cout << "Elementul de pe pozitia " << x << " este: "; for(int i = 0; i < pointer; i++) { if(a[i] == 1) cout << "a"; if(a[i] == 2) cout << "b"; if(a[i] == 3) cout << "c"; } cout << "\n"; return 0;} Quote
m32mark Posted October 24, 2010 Report Posted October 24, 2010 <?phpset_time_limit(0);function wordGenerator($x, $poz){ static $ctr; for($i=0,$z="",$e="if(++\$ctr==\$poz) echo ";$i<16;$i++){ $z.="foreach(\$x as \$y[$i])"; $e.="\$y[$i]."; eval($z.$e."\"<br />\";"); }}wordGenerator(array(a,b,c), 48884560);?> afiseaza: bccabbbaacbbbcba.Script scris la betie.Pentru rezultate de sub o secunda recomand rularea scriptului pe un calculator gen sequoia sau jaguar Quote
tdxev Posted October 24, 2010 Author Report Posted October 24, 2010 Da vad ca rezultatul este bun, da.. chiar ca trebuie un "sequoia" pentru el Quote
m32mark Posted October 24, 2010 Report Posted October 24, 2010 Recunosc ca metoda mea e cam cea mai putin inteligenta, numai ca am incercat sa rezolv intr-un alt fel (ca la scoala ), e o metoda mai economica de linii din script dar nu si la performanta/memorie Quote