अभिव्यक्ति जो इसकी लंबाई के बराबर है


14

किसी संख्या को देखते हुए, उस संख्या की लंबाई के साथ, उस संख्या के बराबर शब्दों में एक अभिव्यक्ति प्राप्त करें।

इस प्रकार, इनपुट करने के लिए 15, आप आउटपुट कर सकते हैं sixteen minus one, जिसमें पंद्रह अक्षर हैं (रिक्त स्थान नहीं)। यदि कई समाधान मौजूद हैं, तो आप जो चाहें प्रिंट करें। यदि कोई मौजूद नहीं है, तो प्रिंट करेंimpossible

उपयोग केवल ऑपरेटरों plus, minus, times, और divided by। ऑपरेटरों को दाएं से बाएं निकाला जाता है।

प्रारूप 1234 के रूप में one thousand two hundred thirty four। "और" की अनुपस्थिति पर ध्यान दें, और यह कि कोई डैश या अल्पविराम नहीं हैं।

इनपुट और आउटपुट में उपयोग किए जाने वाले सभी नंबर 10,000 से कम सकारात्मक पूर्णांक होने चाहिए।

इनपुट को कमांड लाइन तर्क के रूप में दिया जाएगा। मानक आउटपुट पर प्रिंट करें।

उदाहरण

1: impossible
4: four
7: impossible
13: eight plus five (you could also output "five plus eight")
18: one plus two times six (note that operators are evaluated from left to right)
25: one thousand divided by forty

4
गैर-पूर्णांक पूर्णांक? So for 1234 we can do (massive expression) times zero plus one thousand two hundred thirty four.आप शून्य को बाहर करना चाह सकते हैं। आप पर निर्भर करता है।
लेवल रिवर सेंट

@steveverrill अच्छा बिंदु; मैंने इसे "सकारात्मक पूर्णांक" में बदल दिया है।
Ypnypn

4
sooo ... one hundred three times one times one times one times one times one times one times one times one times one times one times oneवैध है?
Qwix

@ क्विक्स हाँ; उबाऊ उत्तर स्वीकार्य हैं, हालांकि वह 104, 105, 106, 107, 108, 109, 110, या 111 के लिए काम नहीं करता है।
Ypnypn

जवाबों:


1

जावास्क्रिप्ट, 434 अक्षर

function f(s){return s.replace(/[A-Z]/g,function(t){return{O:'one',T:'two',H:'three',F:'four',I:'five',S:'six',E:'seven',G:'eight',N:'nine',Z:'ten',P:' plus ',M:' minus ',U:' times ',X:'teen',A:'thir',B:'twenty'}[t]})}A='TPAX|GeenMT|EXUO|B FMS|F|GPSUT|ZPHPG|BPFMT|AXPFPS|BPGMF|EUOPGeen|NPT|B GMSPI|GPI|EPE'.split('|');O=26640;function S(n){return n<15&&!(O>>n&1)?'Invalid':f(A[v=n%15])+(new Array(~~(n/15)+(O>>v&1))).join(f('PSPN'))}

वैश्विक नाम स्थान में एक फ़ंक्शन देता है S, जो किसी भी गैर-नकारात्मक पूर्णांक को स्वीकार करता है और आवश्यक स्ट्रिंग लौटाता है, या "Invalid"यदि पूर्णांक को विनिर्देशों के भीतर प्रतिनिधित्व नहीं किया जा सकता है।

मुझे लगता है कि @optokopper के समान दृष्टिकोण का उपयोग किया गया है, वही अवलोकन किया "plus six plus nine"है जो कम से कम संभव पैडिंग स्ट्रिंग है, और यह कि 27 से अधिक सभी संख्याओं को पैड की दोहराया प्रतियों में 15 आधार तारों में से एक को व्यक्त करके व्यक्त किया जा सकता है।

यह कहने के बाद कि, बेस स्ट्रिंग्स के तालिकाओं का उपयोग हम अलग-अलग करते हैं, और मेरा समाधान बिट ट्विडलिंग और शेष ऑपरेटर ( %) पर निर्भर करता है । इसमें "multiplied by"एक संभावित ऑपरेशन भी शामिल है । और स्वाभाविक रूप से सी और जावास्क्रिप्ट के बीच की असमानता के कारण तार का निर्माण कैसे किया जाता है, इसके बारे में यांत्रिकी पूरी तरह से अलग है।

यह किसी भी दर पर मेरा सबसे अच्छा प्रयास है। ;)

@Chiru के लिए विशेष धन्यवाद, जिनकी संख्याओं की चर्चा विश्वसनीय थी, उन्होंने फलहीन खोज को रोकने में मदद की।


22

जेएस, 1719/1694

सिद्धांत

दुर्भाग्य से, नियम जो आप प्रदान करते हैं वह गणितीय दृष्टिकोण से एक बुद्धिमान निर्णय नहीं हो सकता है। वास्तव में, नियमों के एक छोटे उपसमुच्चय का उपयोग करके, आप दिए गए अंतराल में हर संख्या के लिए एक समाधान पा सकते हैं

मैं = [१;  10000]

के अलावा

एक्स = [1;  3] 5 [5;  १०] 12 {१२}

जिसके लिए कोई उपाय नहीं है।

नियम सेट को कम किया गया

नियमों के निम्नलिखित सबट पर विचार करें:

  • केवल ऑपरेटरों का उपयोग करें plus, minusऔर times
  • आपको अपनी अभिव्यक्तियों में या उसके कई घटनाओं को लागू करने की आवश्यकता नहीं हैplusminus
  • आपको न तो लागू करने की आवश्यकता हैdivision और न ही operator associativity(जैसा कि उनका समाधान सेट पहले नियम द्वारा कवर किया गया है)।

यह काम करने का कारण यह है कि, जैसा कि आपने पहले @Qwix के साथ चर्चा की थी, आप उबाऊ उत्तर देते हैं, अर्थात्, ऐसे भाव जो नियमित अभिव्यक्ति में समाप्त होते हैं ( times one)+$। इसकी अनुमति देने पर, दिए गए अंतराल में प्रत्येक संख्या का एक समाधान होगा।

जब आपने अपनी एक टिप्पणी में उत्तर दिया,

@ क्विक्स हाँ; उबाऊ उत्तर स्वीकार्य हैं, हालांकि वह 104, 105, 106, 107, 108, 109, 110, या 111 के लिए काम नहीं करता है। -

आप बिलकुल सही थे: यह तब काम नहीं करता है जब आप अपनी अभिव्यक्ति को स्वयं संख्याओं one hundred four times one times one …या उन संख्याओं में से किसी एक के साथ शुरू करने का प्रयास कर रहे हों ।

हालांकि, आपकी अभिव्यक्ति एक अभिव्यक्ति के साथ शुरू होती है जिसका मूल्यांकन दिए गए नंबरों में से एक के बराबर होता है, तो आप भाग्य से बाहर हैं। उदाहरण के लिए, ध्यान दें कि 17 + 87वास्तव में है 104, इसलिए हम इस प्रकार लिख सकते हैं 104:

104: seventeen plus eighty seven times one times one times one times one times one times one times one times one times one times one

यह देखने के लिए कि यह सबसेट काम करता है, इस फ़ाइल को इस रूप में सहेजें num.jsऔर यह सुनिश्चित करें कि आपके सिस्टम के लिए एक जावास्क्रिप्ट इंजन स्पाइडरमोंकी को इंस्टॉल किया गया है।

एल्गोरिथ्म

  • आइए हम Kसकारात्मक पूर्णांकों के लिए संपत्ति को परिभाषित करते हैं कि Nअक्षरों की संख्या और मान होने की स्थिति N
  • आइए हम Fएक अभिव्यक्ति के लिए संपत्ति को उसके शब्द रूपांतरण की स्थिति के रूप में परिभाषित करें , जो कि 8kℕ k के साथ इसके मूल्यांकन से कम है। F"भरने योग्य" के लिए खड़ा है और वर्णन करता है कि हम अभिव्यक्ति के शब्द रूपांतरण को लंबाई 8 (यानी " times one") के भावों के साथ भर सकते हैं या नहीं , ताकि परिणामी अभिव्यक्ति को संपत्ति मिल सके N

हम फिर इस प्रकार आगे बढ़ते हैं:

  • इनपुट नंबर को शब्दों में बदलें।
  • जांचें कि इनपुट नंबर में संपत्ति है या नहीं K
    • यदि ऐसा होता है, तो शब्दों को वापस कर दें ( 4इस संपत्ति के साथ एकमात्र संख्या है, दुर्भाग्य से)।
    • यदि यह नहीं है, आगे बढ़ें।
  • सभी दो-संचालन अभिव्यक्तियों के लिए (इस क्रम में जोड़, घटाव और गुणा) जो इनपुट संख्या में परिणाम करते हैं, जांचें कि क्या उनके मूल्यांकन में संपत्ति है K
    • यदि ऐसा होता है, तो शब्दों को वापस करें।
    • यदि ऐसा नहीं है, तो जांच लें कि क्या दो-ऑपरेंड की अभिव्यक्ति में संपत्ति है N
      • यदि ऐसा होता है, तो अभिव्यक्ति को भरें " times one"और जांचें कि क्या परिणामी अभिव्यक्ति के मूल्यांकन में संपत्ति है K
        • यदि ऐसा होता है, तो शब्दों को वापस करें
        • यदि यह नहीं है, आगे बढ़ें
      • यदि यह नहीं है, आगे बढ़ें
  • एक कॉफ़ी पी जाओ

अभ्यास

num.js (स्पाइडरमैन / कमांड लाइन के लिए)

function X(e,t){return e+": "+t}function P(e){var n,t;for(n=1;.5*e+(e%2===0?1:0)>n;++n){if(t=C.s(n)+" plus "+C.s(e-n),t.replace(/\s/g,"").length===e)return t;if(F(e,t)&&e>t.length)return G(e,t)}return!1}function M(e){var t,n;for(t=L;t>1;--t){if(0>t-e)return!1;if(n=C.s(t)+" minus "+C.s(t-e),n.replace(/\s/g,"").length===e)return n;if(F(e,n)&&e>n.length)return G(e,n)}return!1}function F(e,t){return(e-t.replace(/\s/g,"").length)%8===0}function G(r,t){var e,i=(r-t.replace(/\s/g,"").length)/8,n="";for(e=0;i>e;++e)n+=" times one";return t+n}function T(e){var t,n,r;if(F(e,C.s(e)))return G(e,C.s(e));for(t=1,n=1;t<Math.floor(Math.sqrt(e));++t){for(;e>tn;)++n;if(tn===e&&(r=C.s(t)+" times "+C.s(n),r.replace(/\s/g,"").length===e))return r}return!1}function Y(e){var n,r,t;return e===C.s(e).length?X(e,C.s(e)):(n=P(e))?X(e,n):(r=M(e))?X(e,r):(t=T(e),t?X(e,t):X(e,"impossible"))}var L=1e4,C=new function(){return this.o=["","one","two","three","four","five","six","seven","eight","nine"],this.t=["","","twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"],this.T=["ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"],this.s=function(e){return e?this.m(e):"zero"},this.m=function(e){return e>=1e6?this.m(Math.floor(e/1e6))+" million"+(e%1e6!==0?" "+this.Z(e%1e6):""):this.Z(e)},this.Z=function(e){return e>=1e3?this.h(Math.floor(e/1e3))+" thousand"+(e%1e3!==0?" "+this.h(e%1e3):""):this.h(e)},this.h=function(e){return e>99?this.o[Math.floor(e/100)]+" hundred"+(e%100!==0?" "+this.U(e%100):""):this.U(e)},this.U=function(e){return 10>e?this.o[e]:e>=10&&20>e?this.T[e-10]:this.t[Math.floor(e/10)]+(e%10!==0?" "+this.o[e%10]:"")},this};print(Y(0|arguments[0]))

num.js (ब्राउज़रों के लिए)

ऊपर से दिया गया कोड अपने अंतिम कमांड के कारण ब्राउज़रों के लिए काम नहीं कर सकता है, जो दिए गए स्क्रिप्ट से बाहर एक अच्छी कमांड बनाने के लिए कमांड लाइन के तर्क देता है।

अपने ब्राउज़र में सीधे जावास्क्रिप्ट कोड चलाने के लिए, उपरोक्त कोड के इस टुकड़े का चयन करें:

function X(e,t){return e+": "+t}function P(e){var n,t;for(n=1;.5*e+(e%2===0?1:0)>n;++n){if(t=C.s(n)+" plus "+C.s(e-n),t.replace(/\s/g,"").length===e)return t;if(F(e,t)&&e>t.length)return G(e,t)}return!1}function M(e){var t,n;for(t=L;t>1;--t){if(0>t-e)return!1;if(n=C.s(t)+" minus "+C.s(t-e),n.replace(/\s/g,"").length===e)return n;if(F(e,n)&&e>n.length)return G(e,n)}return!1}function F(e,t){return(e-t.replace(/\s/g,"").length)%8===0}function G(r,t){var e,i=(r-t.replace(/\s/g,"").length)/8,n="";for(e=0;i>e;++e)n+=" times one";return t+n}function T(e){var t,n,r;if(F(e,C.s(e)))return G(e,C.s(e));for(t=1,n=1;t<Math.floor(Math.sqrt(e));++t){for(;e>tn;)++n;if(tn===e&&(r=C.s(t)+" times "+C.s(n),r.replace(/\s/g,"").length===e))return r}return!1}function Y(e){var n,r,t;return e===C.s(e).length?X(e,C.s(e)):(n=P(e))?X(e,n):(r=M(e))?X(e,r):(t=T(e),t?X(e,t):X(e,"impossible"))}var L=1e4,C=new function(){return this.o=["","one","two","three","four","five","six","seven","eight","nine"],this.t=["","","twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"],this.T=["ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"],this.s=function(e){return e?this.m(e):"zero"},this.m=function(e){return e>=1e6?this.m(Math.floor(e/1e6))+" million"+(e%1e6!==0?" "+this.Z(e%1e6):""):this.Z(e)},this.Z=function(e){return e>=1e3?this.h(Math.floor(e/1e3))+" thousand"+(e%1e3!==0?" "+this.h(e%1e3):""):this.h(e)},this.h=function(e){return e>99?this.o[Math.floor(e/100)]+" hundred"+(e%100!==0?" "+this.U(e%100):""):this.U(e)},this.U=function(e){return 10>e?this.o[e]:e>=10&&20>e?this.T[e-10]:this.t[Math.floor(e/10)]+(e%10!==0?" "+this.o[e%10]:"")},this}

अब, इसे अपने ब्राउज़र के जावास्क्रिप्ट कंसोल में पेस्ट करें, ताकि आप उदाहरण के लिए अपने ब्राउज़र से उसी परिणाम का उत्पादन कर सकें:

Y(1234);

उदाहरण (कमांड लाइन)

chiru@chiru ~ $ js num.js 28
28: fourteen plus fourteen times one
chiru@chiru ~ $ js num.js 7
7: impossible
chiru@chiru ~ $ js num.js 42
42: nine thousand sixty minus nine thousand eighteen

और व्यवस्था चाल जिसके साथ आप प्रत्येक संख्या काम कर सकते हैं देखने के लिए, बस पर एक नजर है उबाऊ जवाब के लिए js num.js 1337:

1337: ten plus one thousand three hundred twenty seven times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one times one

प्रदान किए गए कोड दिए गए अंतराल के लिए मान्य समाधान उत्पन्न करते हैं (और शायद ऊपर भी, आपको केवल चर का मूल्य बढ़ाना होगा L)।

आंकड़े

मेरी रुचि थी कि "कैसे उबाऊ " भाव थे (या: times oneइस एल्गोरिथ्म के भीतर अभिव्यक्ति के अनुसार कितना सबस्ट्रिंग का उपयोग किया गया था), क्योंकि यह भाग दिए गए अंतराल के भीतर हर संख्या के लिए समाधान खोजने के लिए जिम्मेदार था। खुद के लिए देखें:

एक्स : एन-वें अभिव्यक्ति (न्यूनतम 0, अधिकतम 10,000)

y : अभिव्यक्ति के भीतर "बार एक" के घटित होने की संख्या (न्यूनतम 0, अधिकतम 1245)।

ग्राफ़

निष्कर्ष:

  • भाव एक रैखिक तरीके से अधिक से अधिक उबाऊ हो जाते हैं।
  • 99% से अधिक समाधान उबाऊ हैं।

2
4 के लिए एक समाधान मौजूद हैfour
FUZxxl

@FUZxxl मैंने इससे कभी इनकार नहीं किया। यदि आप जवाब दे रहे हैं If it does, return the words (4 is the only number with this property, unfortunately), तो आपको इस सेक्शन के बारे में गलतफहमी हो सकती है। यह कहता है कि 4एकमात्र ऑपरेटर-मुक्त अभिव्यक्ति है जो अपना समाधान बनाती है।
चिरु

@FUZxxl ओह, ठीक है। मैंने अभी देखा कि शुरुआत खंड में, मैंने कहा कि X = [0 में कोई समाधान नहीं हैं; 10] 12 {12}, हालांकि बाद में, मैं कहता हूं कि 4इसका एक समाधान है। मैंने अंतराल को ठीक किया, धन्यवाद। :)
चिरु

6

C, 450 वर्ण

संपादित करें: हटा दिया गया zero

संपादित करें: केवल plusऔर का उपयोग करminus

मैंने सबसे छोटी अभिव्यक्ति की खोज की, जो चार्ट जोड़ता है और स्थिति को सच रखता है। मैंने पाया plus ten plus fiveकि 15 लंबा है और स्ट्रिंग में 15 जोड़ता है।

मुझे पहले 15 नंबरों के लिए केवल अभिव्यक्ति की आवश्यकता है जो किसी भी संभावित संख्या को व्यक्त करना असंभव नहीं है। 12 सबसे बड़ी असंभव संख्या है, इसलिए यह हार्डकोड संख्या 28 से छोटा है।

4 = चार
११ = छः प्लस पाँच
13 = आठ प्लस पांच
14 = बीस माइनस छह
15 = बीस घटा पाँच
16 = अठारह शून्य से दो
17 = चौदह प्लस तीन
18 = बाईस घटा चार
20 = बत्तीस माइनस बारह
21 = बीस प्लस दो माइनस एक
22 = बीस प्लस चार माइनस दो
23 = तीस माइनस आठ प्लस एक
24 = बीस प्लस आठ माइनस चार
25 = बीस प्लस आठ माइनस तीन
२ = अट्ठाईस माइनस छह प्लस पांच

हम हर संख्या> 27 को x * 15 + से ऊपर की संख्या में लिख सकते हैं।

golfed

#define P" plus "
#define M" minus "
#define U"four"
#define F"five"
#define E"eight"
#define W"twenty"
#define A"ten"P F P
*e[]={0,0,0,0,U,0,0,0,0,0,0,F P"six",0,E P F,W M"six",W M F,E"een"M"two",U"teen"P"three",W" two"M U,A U,"thirty two"M"twelve",W P"two"M"one",W M"two"P U,"thirty"P"one"M E,W P E M U,W M"three"P E,A F P"six",W" "E M"six"P F};main(n){n=atoi(1[(int*)1[&n]]);for(printf("%d: ",n);n>27;n-=15)printf(A);puts(e[n]?e[n]:"impossible");}

पठनीय संहिता

#include <stdio.h>
#include <stdlib.h>

// add fifteen to string, both as value and as character count (without spaces)
const char *add_fifteen = "plus ten plus five";

// table with hardcoded expressions
// NOTE: we could calculate 19, 26, 28 and 29 from 4, 11, 13 and 14
// but we would need more logic, so we hardcode those 4 numbers too.
const char *expressions[30]={"impossible", "impossible", "impossible", "impossible",
    "four", "impossible", "impossible", "impossible", "impossible",
    "impossible", "impossible", "five plus six", "impossible",
    "eight plus five", "twenty minus six",
    "fourteen plus one", "eighteen minus two", "fourteen plus three",
    "twenty two minus four", "four plus ten plus five",
    "thirty two minus twelve", "nine plus seven plus five",
    "twenty plus four minus two", "twelve plus seven plus four",
    "twenty plus eight minus four", "twenty plus eight minus three",
    "five plus six plus ten plus five", "twenty eight minus six plus five",
    "eight plus five plus ten plus five", "seven plus seven plus ten plus five"};

int main(int argc,char *argv[])
{
    int n = strtol(argv[1], NULL, 0);
    int fifteens = 0;

    printf("%d: ", n);

    // how many times do we need to add fifteen?
    if(n>29){
        fifteens=(n/15) - 1;
        n -= fifteens*15; // ensure 30 > n >= 15, so we don't get "impossible"
    }

    // look up the expression for n
    printf("%s", expressions[n]);

    // add fifteens till we are done
    while(fifteens-- > 0) {
        printf(" %s", add_fifteen);
    }

    printf("\n");
    return 0;
}

2
यह निश्चित नहीं है कि आपका कोड कैसे काम करता है, लेकिन चूंकि प्रश्न कहता है कि all numbers used in the output must be positive integers, क्या आप #define Z "zero"अपने कोड से Z के उदाहरणों के साथ हटा सकते हैं क्योंकि आपको कभी भी इसका उपयोग नहीं करना चाहिए?
Qwix

"प्लस बारह" 12 अक्षर हैं। क्या यह आपके कोड को छोटा करने में मदद करेगा?
isaacg

मैं इसे कमतर बना plus twelve
दूंगा, अलस

ठीक है, मैंने नियमों को गलत बताया।
isaacg
हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.