区分大小写重要吗?


13

汤姆将实现他发明的一种新的编程语言。但是在实际开始研究之前,他想知道他的语言是否应该区分大小写。

一方面,对他而言,不区分大小写似乎更容易实现,但他担心这可能会导致缺少形成变量的字符组合的可能性,这意味着应使用更长的变量名来避免命名冲突(因为例如,你可以使用HelloHElloheLLo和一堆其他的可能性,如果语言是大小写敏感的,但只HELLO如果没有)。

但是汤姆是一个细心的人,所以仅仅担心对他来说还不够。他想知道数字。

挑战

编写一个函数(或者完整的程序,如果您的语言不支持它们的话),给定一个整数n作为输入,输出(或返回)一个长度字符串的可能排列数量的差异(n不区分大小写)。

用汤姆的语言,变量名可以包括所有字母,下划线,以及从第二个字符开始的数字。

测试用例

Input (length of the variable) -> Output (difference between the possibilities with case sensitivity and the possibilities with case insensitivity)
0 -> 0
1 -> 26
2 -> 2340
5 -> 784304586
8 -> 206202813193260
9 -> 13057419408922746

非竞争性C ++参考实现

void diff(int n) {
    long long total[2] = {0, 0}; //array holding the result for case insensivity ([0]) and case sensitivity ([1])

    for (int c = 1; c <= 2; c ++) //1 = insensitivity, 2 = sensitivity
        for (int l = 1; l <= n; l ++) //each character of the name
            if (l == 1)
                total[c - 1] = 26 * c + 1; //first character can't be a number
            else
                total[c - 1] *= 26 * c + 1 + 10; //starting from the second character, characters can include numbers

    std::cout << total[1] - total[0] << std::endl;
}

计分

汤姆喜欢高尔夫,所以最短的节目以字节为单位获胜。

注意

没关系,如果后两个测试用例由于数值精度而不合适。毕竟,我什至不确定我的代码是否正确处理了9号。


4
测试用例5似乎缺少最后三位数字。
丹尼斯

1
规范说只能使用偶数,但是输出暗示允许使用10位数字吗?
feersum'7

@Dennis哦,谢谢,我忘了;)
user6245072

19
“不区分大小写似乎更容易实现”让我觉得汤姆真的没有想到这通过..

2
@dorukayhan我无法评论您的问题,因为我不属于本网站,但在这种情况下,不,因为所提供的代码易于打高尔夫球,所以C ++不被禁止,甚至还有其他更短的方法来应对挑战。
user6245072 2016年

Answers:


5

果冻16 15 字节

“~J“6j”OH*"’ÆḊḞ

在线尝试!验证所有测试用例

怎么运行的

“~J“6j”OH*"’ÆḊḞ  Main link. Argument: n

“~J“6j”          Yield ['~J', '6j'].
       O         Ordinal; replace the characters with it code points.
                 This yields [[126, 74], [54, 106]].
        H        Halve, yielding [[63, 37], [27, 53]].
           ’     Yield n-1.
         *"      Exponentiation (zipwith); elevate 63 and 37 to the power n-1.
            ÆḊ   Take the determinant of the matrix
                 [[63 ** (n-1), 37 ** (n-1)], [27, 53]], thus yielding
                 63 ** (n-1) * 53 - 37 ** (n-1) * 27.
              Ḟ  Floor; round the result down to the nearest integer.
                 This is required for n = 0.

11

JavaScript(ES7),43个字节

f=(n,a=53,b=27)=>n?--n?f(n,a*63,b*37):a-b:0
<input type=number value=0 min=0 max=9 style=width:4ch oninput=o.textContent=f(+this.value)><div id=o>0</div>

reduce今天不是一天-55个字节。


今天不是您的一天,煎饼!

看来您的公式错误。这不会给出正确的结果。尝试2作为输入。
t-clausen.dk

@ t-clausen.dk抱歉,我有错字了。立即修复。
尼尔


5

Java,77 76字节

O(1)解决方案,不涉及循环

long x(long n){return 53*(long)Math.pow(63,n-1)-27*(long)Math.pow(37,n-1);}

不错,但输入0错误
edc65 '16

3
我的错,对不起。似乎错误,因为指数<0的Math.pow不会为您提供整数结果。我没有考虑过(长)转换。Upvote
edc65 '16

1
之后还有一个额外的空间n-1

6
pow功能不是固定时间
pppery

1
您可能只需将整个返回值转换为即可long
PurkkaKoodari'7

4

JavaScript(ES6)47

n=>n&&eval("for(a=53,b=27;--n;a*=63)b*=37;a-b")

注意:这些值是正确的,最高为JavaScript数字的53位精度。

测试

F=n=>n&&eval("for(a=53,b=27;--n;a*=63)b*=37;a-b")

for(i=0;i<=10;i++)O.textContent+=i+' '+F(i)+'\n'
<pre id=O></pre>


3

MATL17 16字节

37iqY"27vt26+hpd

在线尝试!

由于数值精度,最后一个测试用例(输入9)不精确。

说明

37      % Push number 37
iq      % Input number n and subtract 1
Y"      % Row array [37 ...37] of size n-1
27v     % Concatenate 27 vertically. Gives column array [37; ... 37; 27]
t26+    % Duplicate and add 26. Gives column array [63; ... 63; 53]
h       % Concatenate horizontallty. Gives 2D array [37 63; ... 37 63; 27 53]
p       % Product of each column
d       % Difference of the two values. Implicitly display

3

Python 3,36个字节

lambda n:n and 53*63**~-n-27*37**~-n

一个匿名函数,它通过参数获取输入并返回输出。

祝运营商优先!

怎么运行的

lambda n:   Function with input string length n
n and ...   Base case: return 0 if n=0

(Case sensitive)

53*...      The first character can be any of [a-z], [A-Z] and _ , giving 53 possibilities
...63**~-n  63^(n-1): the remaining n-1 characters can be any of above and [0-9], giving
            63 possibilities

(Case insensitive)

27*...      The first character can be any of [A-Z] and _ , giving 27 possibilities
...37**~-n  37^(n-1): the remaining n-1 characters can be any of above and [0-9], giving
            37 possibilities
...-...     Subtract the case insensitive value from the case sensitive value
:...        Return the above

在Ideone上尝试


1
恭喜,这实际上是相当不错的。
user6245072


2

因子,54字节

[ 1 - [ 63 swap ^ 53 * ] [ 37 swap ^ 27 * ] bi - ⌊ ]

测试输出(太懒了,无法立即创建测试套件)

在此处输入图片说明

到100,因为我可以:

0   0
1   26
2   2340
3   173394
4   11884860
5   784304586
6   50726849940
7   3244471004034
8   206202813193260
9   13057419408922746
10  825083183338785540
11  52071473691679633074
12  3283878468805254213660
13  207009241705220196670506
14  13046203459736972095009140
15  822081803558828931061964514
16  51797480091236011152730146060
17  3263475325027970876967575323866
18  205607606410125945699743561920740
19  12953599658372394455762938112142354
20  816088635295235866150191568376990460
21  51414022725857535138635748098295218826
22  3239099663712558709867478263942757120340
23  204063879397281956578587897577149556950594
24  12856046623614221305157697924464388847546860
25  809931759486357889731081403194103267502851386
26  51025731268991039110785546457483836346653407940
27  3214622195536403670115403894902928918116194213234
28  202521239965622254844299280697898069287570327415260
29  12758839658766868529390921590771079620628184030477546
30  803806955516821376897030535770307562553491969842783540
31  50639840307096567147692815348943369659664900009235194274
32  3190310017399946085222303356013750037654300219219990995660
33  200989533984152510500958383022988009088751140000471861713306
34  12662340747855976725442649179430749571102940189045387160447140
35  797727471068538170566530927116489907924415111563438806390997714
36  50256830823601535309646277474395913542200557575679083168097488060
37  3166180347299391055374044156331053378848244132513532459112333134666
38  199469362080123926730618942840288463417954913542445303046398068798740
39  12566569818457512122984997355621160915700234281355486142448958342967554
40  791693898836982339089426979801403683342770524668108612843741935926892460
41  49876715636873773150264669144187442239779806356971223086325671695231877626
42  3142233085498371482609012624501672238105982542695761132093804716877601438340
43  197960684400284383047634318675066295949671525651476192195155330966174637087794
44  12471523117731734378801823439793231607942107258123800020604874301575774756008860
45  785705956436110540996146747147743624935526399518788998053580353675424396549238186
46  49499475256178381262627624276616339615439587933192321957328073370569308698667165940
47  3118466941165264455200744360060243571819246756040935041269911532639516601510442382434
48  196463417294374638796889443817231669538334996131822201644459414237154601574451074437260
49  12377195289581232434616009278422739187922835424850800583245777641132746959324266250272346
50  779763303244935960426051634304306897098424666501802506291342869526467319662381170513981540
51  49125088104479743237515245802427284662794337274853034469588379271166298804053252422013315474
52  3094880550584028599998398220679389089143005829869601804793717698250434558272314733733240577660
53  194977474686860578513191784102480908365326982799277757130761257743954813314983341977611019396106
54  12283580905274687184722912157844434869740351690501733906101969819773718376165568553428918153085140
55  773865597032396709958041167041560489574457972145056938738392490176213167779330685193080529096190914
56  48753532613044375168215008464220671276081037423946152138715557367728779243091128221265244694777630060
57  3071472554621920785909306886048189626410042209324487489672362842172125030215492994941453756154541985466
58  193502770941185640073821503874720577896459322697230268331890319993036718605903879610376090231927542876740
59  12190674569294866655427556036093727770484123879787646494762754214200448415158073789829080317596516919552754
60  768012497865582938530677678077399109971765706771520893994639085516276250445445435611121365429028589509594460
61  48384787365531959679266134687605431564178077913367085420171928478326379788811073557020550322585378011481976426
62  3048241604028522138211603801162125831073621928852293338115689139494198039092774045292531129443472159001332956340
63  192039221053797215808791020159404322131263093269170657697148148666441012621558792067838210142400699755368808584994
64  12098470926389236476707853555431516900893696610762369998567143482483125633030622907206930951507336372904773737270860
65  762203668362522337620493487551580215201395390665800196764662013706838562882208746410562227308797606160712680915464986
66  48018831106838923534843342117447155624156332266892935209806189913015690437626492644356866682916159530830240509968123940
67  3025186359730852784490963892261982080781180571047313262322191837350928353684220676552666116434808733122402791782863511634
68  190586740663043747689376558750428888318209646590603894258160955047853260958314713301270755405603305445874988549550654259260
69  12006964661771756928289219042180208601520032747949102211343066331920312103393552933082810823150791387679177947309210580307146
70  756438773691620716964985145770771121482256588592212543618533446975485259045523696603018951464617833773583196421918482952379540
71  47655642742572106296656344989755045898082462523751897107212657077842278391541997773285863117697288652678155847014078432541996674
72  3002305492782042738420254124183837105633106144366742271546464242884371700319084040546949135905704694741593153841915859492202959660
73  189145246045268694064519472247264698574880394293808554997733720639986819101224007245165566663218089624766534104801769122964901718906
74  11916150500851927783194334861253945564257269006861979264798563913835211476678615826001618230525611319033999768874671043820165072923140
75  750717481553671452455038596317020544047680701587330184780139088571711872342908421711680887160599764988069185889174180556385321385544114
76  47295201337881301582877865070119107294484376103737744811768456374021309282152988680758316620704776514138665127670559852493727353570572060
77  2979597684286522002615091538996935841273293894973707820454341421941470553783979337065903514099119599226677240459390970372438551207353876266
78  187714654110050886271820850421245945023886308799558098889201870386303383441699317091742715367049125868210495633339022021080976659562384154740
79  11826023208933205839086306664722737056380582810772096959441117184084770483299475874473650445339864801023664915823061850169943403091896529897954
80  745039462162791968009016264140349407787379295265439767433382158538003861527346479306512775013394967703567826260992924685854583715749735525096460
81  46937486116255893989991445578566240700314790994634218730880971697698786156163569667253198313252271919170619707315735295839320298967762741317515226
82  2957061625324121321570127646145469600479097962199681775200883358217791614396112323461838552769003522200045367717199022141204995255363640622751674340
83  186294882395419643266342704970909052975476018411475813658404790794443290909593951451816650008709306376417822233966923239519043908642509112264039442194
84  11736577590911437526054302953925815658830824491260123147847219971438656837802057319192119334366383427393476471247901403340755546923998264934769791332860
85  739404388227420564151585450105392563397247850208862193146980429802018372669942231085027322266336949575917703497413242262756663341354137752789503193531786
86  46582476458327495541925964824938180038990133131758872257066173226778328178077627497465902058225655188337576695262466055088365154255573819716001935756281940
87  2934696016874632219155250798298147938620028574339029453481005940809327991114127409087391517619734089372293502673776337794350733176860886869847861631505600834
88  184885849063101829807295655822883896191116857103773014116879344289312516128313791212146134064249406693214458990720825405024094143116346113226785650902666881260
89  11647808490975415277878675971455406774188399103593023755623709580904708065544348130631903778853340506994629744339509897103777855276371884029063199627227132581946
90  733811934931451162507061423422398315397346516450407479655925206552083331459335365747677739381568683697580070526580745691266622080032985612974682610468596757977540
91  46230151900681423237970948652777278349101493334565409591233653622119458645149661082264806229649731647953525118602676998977756027354075699625721942715793229749237874
92  2912499569742929663993134687280117361718934603610641124045403705739039618883255370668643812466536563096293367462807981691433106666850680498728209106577023932078141660
93  183487472893804568831603187418387900340137879398192142647374723980743503786481677084105117925340131838249669694817958084526161366922717594045263062187188374662340681706
94  11559710792309687836392321785788836463846951378802809804587636359996649027031299439381903065674516455247507129925990403129885565051842823161990850791287794680559919961140
95  728261779915510333692765148706621450691833741003095095947733154400551795376841154655141276683483798209790732933978380017958074358887427604479576881170443366718075867057314
96  45880492134677151022646012787988367271956130436320159940279535084902990655674156472314911622261081963797134173540354372100044183752897139657357014922552487271422413276314060
97  2890471004484660514426765717163702125632948593353801325373787525582612830543998915730356846276907462752691218884931833388144147044723120219693807782246945239322406481520807066
98  182099673282533612408888715907569328452365119288317839716587156275352411836023432836069625636200164217658002129970617297449211712144308789428081576440224676102555002804992632740
99  11472279416799617581760080704048343190386108757724073082212416905402170675604281811039500754948545126089276981776285626117157164453181285710701891703604838257394970772074262003154
100 722753603258375907650888473624290214416147782711338423841877029262370595570657559163071778136871929817566895212667053691361603944309744782913331015678320105744441365668988373398460

那是我从未听说过的语言。所有这些空间真的必要吗?
user6245072

@ user6245072我联系的语言,是的,需要的空间:-)

@ user6245072喜欢大数字吗?:D

我更喜欢小数uu
user6245072 '16

1
@ user6245072我必须承认-如果您键入其中一个错误,则编译错误可能有点难以理解...
Toby Speight

2

dc,19个字节

1-d63r^53*r37r^27*-

这只是两个指数的差53 × 63^(n-1) - 27 × 37^(n-1)。零的特殊情况自然而然地消失了,因为dc精度以开头0,这意味着对于所有正xx ^ -1 == 。0

正常情况下dc,输入是从堆栈的顶部获取的,而输出则被压入堆栈的顶部。对于完整的程序,请在之间?加上p并组成管道过滤器。

测试输出

$ for i in {0..30}; do printf '%2d -> %s\n' "$i" `./85108.dc <<<$i`; done
 0 -> 0
 1 -> 26
 2 -> 2340
 3 -> 173394
 4 -> 11884860
 5 -> 784304586
 6 -> 50726849940
 7 -> 3244471004034
 8 -> 206202813193260
 9 -> 13057419408922746
10 -> 825083183338785540
11 -> 52071473691679633074
12 -> 3283878468805254213660
13 -> 207009241705220196670506
14 -> 13046203459736972095009140
15 -> 822081803558828931061964514
16 -> 51797480091236011152730146060
17 -> 3263475325027970876967575323866
18 -> 205607606410125945699743561920740
19 -> 12953599658372394455762938112142354
20 -> 816088635295235866150191568376990460
21 -> 51414022725857535138635748098295218826
22 -> 3239099663712558709867478263942757120340
23 -> 204063879397281956578587897577149556950594
24 -> 12856046623614221305157697924464388847546860
25 -> 809931759486357889731081403194103267502851386
26 -> 51025731268991039110785546457483836346653407940
27 -> 3214622195536403670115403894902928918116194213234
28 -> 202521239965622254844299280697898069287570327415260
29 -> 12758839658766868529390921590771079620628184030477546
30 -> 803806955516821376897030535770307562553491969842783540

对于小n来说,性能是合理的,但是一旦达到几十万左右,它的确会开始降低:

   1024:   0.00s
   2048:   0.00s
   4096:   0.01s
   8192:   0.04s
  16384:   0.12s
  32768:   0.38s
  65536:   1.16s
 131072:   3.48s
 262144:  10.41s
 524288:  30.63s
1048576:  93.70s

最后一个示例的确产生了1941435位的结果,因此对于所有情况来说可能还不错。


它的数字大小是多少?OO
user6245072 '16

我的意思是,在我的C ++实现中,我使用了8个字节的整数,最多可以容纳2 ^ 64个值,这是相当多的。但是它已经无法计算测试用例11,因为结果超过了这些字节的容量。那么,您的语言所占的数字大小如何才能存储测试用例11及更高版本的结果?
user6245072

1
dc使用任意精度算术,因此它仅受可用内存的限制(尽管一旦n达到100,000左右,它确实开始变慢)。我将添加一些数字。
Toby Speight

干得好,伙计!我只是在浏览以查看是否dc已被使用过……但是不能被打败!

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.