क्या यह संख्या एक पहाड़ी संख्या है?


17

एक पहाड़ी संख्या एक ऐसी संख्या है जिसमें पहले और आखिरी में एक ही अंक होता है , लेकिन यह सब नहीं है। एक पहाड़ी संख्या में पहले अंक सख्ती से बढ़ रहे हैं , और अंतिम अंक सख्ती से कम हो रहे हैं सबसे बड़ा अंक दोहराया जा सकता है

यहाँ एक पहाड़ी संख्या का उदाहरण दिया गया है:

12377731 | 1237...             | ...731
^ same ^ | strictly increasing | strictly decreasing 
---------+---------------------+---------------------
12377731
   ^^^ okay because largest digit can be repeated

यह नहीं है :

4588774 | ...8774
        |     ^^ not the largest digit
        |        so this has to be strictly decreasing
        |        but it's not, so not a hill number

चुनौती

एक सकारात्मक पूर्णांक को देखते हुए, एक पूर्ण कार्यक्रम या एक फ़ंक्शन लिखें जो पहाड़ी संख्याओं के लिए सच्चाई देता है लेकिन अन्य मूल्यों पर मिथ्या है।

टिप्पणियाँ:

  • इनपुट और आउटपुट किसी भी उचित प्रारूप में हो सकते हैं ।
  • यह इसलिए प्रत्येक भाषा में सबसे कम जवाब जीतता है!

परीक्षण के मामलों

12321 -> Truthy
1233321 -> Truthy
99 -> Truthy
3 -> Truthy
234567992 -> Truthy
1232 -> Falsy
778896 -> Falsy
23232 -> Falsy
45566554 -> Falsy
5645 -> Falsy

5
किस बारे में 222222222? क्या यह समतल पहाड़ी संख्या है?
frarugi87

1
222222222एक पहाड़ी संख्या है, सबसे बड़ा अंक 2 है और इस प्रकार दोहराया जा सकता है
u_ndefined

1
क्या एक स्ट्रिंग उचित है?
Sanchises

@ frarugi87 ऊपर टिप्पणी देखें।
डेनिस

है 1230321एक पहाड़ी संख्या?
HelloGoodbye

जवाबों:


10

जेली , 8 बाइट्स

_ƝṠÞ+SƊƑ

इसे ऑनलाइन आज़माएं!

यह काम किस प्रकार करता है

_ƝṠÞ+SƊƑ  Main link. Argument: n (integer)

_Ɲ        Take the differences of neighboring digits.
          This maps n = abcd to [a-b, b-c, c-d].
       Ƒ  Fixed; apply the link to the left and return 1 if the result is equal to
          its argument, 0 if not.
      Ɗ       Drei; combine the three links to the left into a monadic chain.
  ṠÞ              Sort the differences by their signs (negative, zero, positive).
     S            Take the sum of the differences, yielding 0 if and only if the
                  first digit is equal to the last.
    +             Add the sum to each difference.

6

जावास्क्रिप्ट (ईएस 6), 62 54 बाइट्स

एक स्ट्रिंग के रूप में इनपुट लेता है। एक बूलियन मान लौटाता है।

s=>s[-[...s].some(p=q=n=>q>(q=Math.sign(p-(p=n))))]==p

इसे ऑनलाइन आज़माएं!

टिप्पणी की गई

s =>                  // s = input string
  s[                  // we will eventually access either s[0] or s[-1]
    -[...s].some(     // depending on the result of this some()
      p = q =         // initialize p and q to non-numeric values
      n =>            // for each digit n:
        q > (         //   compare q with
          q =         //   the new value of q,
          Math.sign(  //   defined as the sign of
          p - (p = n) //   the difference between the current digit and the previous one
        ))            //   yield true if the previous q is greater than the new q
    )                 // s[-1] being undefined, a truhty some() will force the test to fail
  ] == p              // otherwise: test if the 1st digit s[0] is equal to the last digit p

जावास्क्रिप्ट (ईएस 6), 65 बाइट्स

एक नियमित अभिव्यक्ति का उपयोग कर एक समाधान। एक स्ट्रिंग के रूप में इनपुट लेता है। रिटर्न 0 या 1

s=>/N(,-\d+)*(,0)*[^0-]*$/.test([...s].map(p=v=>p-(p=v)))&p==s[0]

इसे ऑनलाइन आज़माएं!

कैसे?

हम पहले संख्या को जोड़-तोड़ अंकों के अंतर की सूची में बदल देते हैं [-9,9] :

[...s].map(p = v => p - (p = v))

उदाहरण:

"234567992" --> [ NaN, -1, -1, -1, -1, -1, -2, 0, 7 ]

यह सरणी एक स्ट्रिंग के लिए मजबूर है, जो देता है:

"NaN,-1,-1,-1,-1,-1,-2,0,7"

हम निम्नलिखित नियमित अभिव्यक्ति लागू करते हैं:

 +-----------------------> the second 'N' of 'NaN'
 |    +------------------> a sequence of negative numbers
 |    |     +------------> a sequence of zeros
 |    |     |     +------> a sequence of positive numbers
 |    |     |     |  +---> end of string
 |    |     |     |  |
 |/¨¨¨¨¨¨\/¨¨¨\/¨¨¨¨\|
/N(,-\d+)*(,0)*[^0-]*$/

अंत में, हम यह भी परीक्षण करते हैं कि अंतिम अंक pपहले अंक के बराबर है या नहीं s[0]


आप अंकों की एक सरणी के रूप में इनपुट लेकर 5 बाइट्स बचा सकते हैं।
झबरा

@ शैगी की इच्छा है कि मैं कर सकता हूं लेकिन यह स्पष्ट रूप से अनुमति नहीं है
अरनौलद

मूल जोर के साथ कल्पना से: "इनपुट और आउटपुट किसी भी उचित प्रारूप में हो सकते हैं " - हम आमतौर पर एक अंक सरणी को पूर्णांक के लिए एक उचित प्रारूप मानते हैं।
झबरा

4

पायथ, 16 बाइट्स

&SI_._MJ.+jQT!sJ

परीक्षण सूट की कोशिश करो

          jQT          input in base 10
       J.+             J = differences: [3,1,4,1] -> [-2,3,-3]
    ._M                Signs of each element of J
   _                   Reverse the list
 SI                    and check if it is Invariant under Sorting.
                       If this is true, J consists of some positive numbers,
                         followed by some 0s, followed by some negative numbers,
                         which is what we want.
            !sJ        Now we check the other hill condition by ensuring
                         sum(differences) = 0; i.e. the first and last digit are equal.
&                      We take the logical AND of both conditions.

4

जेली , 11 बाइट्स

DIµṠNṢƑaS¬$

स्पष्टीकरण:

D               Convert to a list of Digits.
 I              Increments; compute differences between successive elements.
  µ             Start new µonadic link.
   Ṡ              Find Ṡign of each increment
    N             then negate;
     ṢƑ           is the result invariant under Ṣorting?
                  If so, the increments consist of some positive numbers,
                     followed by some 0s, followed by some negative numbers,
                     which is what we want.
       a          Logical AND this result with
        S¬$       logical NOT of the Sum of the increments.
                  If the sum of the increments is zero, first and last digits are equal.

इसे ऑनलाइन आज़माएं!


4

पर्ल 6 , 39 बाइट्स

{.[0]==.tail&&[<=] $_ Z<=>.skip}o*.comb

इसे ऑनलाइन आज़माएं!

व्याख्या

{ ... }o.comb  # Split into digits and feed into block
.[0]==.tail    # First element equals last
&&             # and
     $_ Z<=>.skip  # Pairwise application of three-way comparator
[<=]           # Results never decrease

मैं सचमुच इस लोल को पोस्ट करने से कुछ सेकंड दूर था ।
जो राजा



2

05AB1E , 19 17 13 12 बाइट्स

¥D.±Â{RQsO_*

-5 बाइट्स @lirtosiast के पायथ उत्तर का एक पोर्ट बनाकर ।

इसे ऑनलाइन आज़माएं या सभी परीक्षण मामलों को सत्यापित करें

स्पष्टीकरण:

¥           # Push the deltas of the digits of the (implicit) input
            #  i.e. 4588774 → [1,3,0,-1,0,-3]
 D          # Duplicate this list
          # Get the sign of each
            #  [1,3,0,-1,0,-3] → [1,1,0,-1,0,-1]
    Â       # Bifurcate (short for DR: Duplicate and Reverse copy)
            #  i.e. [1,1,0,-1,0,-1] → [-1,0,-1,0,1,1]
     {      # Sort the copy
            #  i.e. [-1,0,-1,0,1,1] → [-1,-1,0,0,1,1]
      R     # Reverse it
            #  i.e. [1,1,0,0,-1,-1]
       Q    # And check if they are equal
            #  i.e. [1,1,0,-1,0,-1] and [1,1,0,0,-1,-1] → 0 (falsey)
s           # Swap to get the list of deltas again
 O          # Take the sum
            #  i.e. [1,3,0,-1,0,-3] → 0
  _         # And check if it's exactly 0
            #  0 → 1 (truthy)
*           # Check if both are truthy (and output implicitly)
            #  i.e. 0 and 1 → 0 (falsey)

Â{RQवैकल्पिक रूप (Â{Qसे उसी बाइट-काउंट के लिए हो सकता है , जहां (प्रत्येक चिह्न को नकारता है: इसे ऑनलाइन आज़माएं



2

MATL , 12 बाइट्स

dZSd1<AGds~*

इसे ऑनलाइन आज़माएं!

व्याख्या

इनपुट अंकों की एक स्ट्रिंग है। आउटपुट एक है 1या 0। संख्या 222222इस कार्यक्रम के अनुसार एक पहाड़ी संख्या है। पहले और आखिरी अंकों की समानता की जाँच के लिए डेनिस विधि की नकल करके 2 बाइट्स सहेजे।

d               % Takes the difference between digits
 ZS             % Calculate the sign. 
   d            % Take the difference again. 
    1<          % A number is a hill number if these differences are < 1.
      A         % Truthy iff above is all true OR if array is empty (necessary for short inputs)
       Gds      % Push the input, and sum all the differences.
          ~     % Negate
           *    % Multiply the two tests (=logical AND).

1

पायथन 2 , 53 बाइट्स

def f(s):x=map(cmp,s,s[1:]);s[:sorted(x)==x]!=s[-1]>_

एक स्ट्रिंग के रूप में इनपुट लेता है। आउटपुट एक अपवाद की उपस्थिति या अनुपस्थिति के माध्यम से है

इसे ऑनलाइन आज़माएं!


पायथन 2 , 62 बाइट्स

lambda s:s[:eval('<='.join(map(str,map(cmp,s,s[1:]))))]==s[-1]

एक स्ट्रिंग के रूप में इनपुट लेता है और एक बूलियन देता है।

इसे ऑनलाइन आज़माएं!


वाह, मैं अपने सिर पर घंटों तक चोट कर रहा हूं और मैं आपके 2 समाधानों की संयुक्त बाइट की तुलना में कुछ कम नहीं कर सका! चीयर्स।
इथेन

1

गणितज्ञ / वुल्फ्राम भाषा, 69 64 बाइट्स

शुद्ध कार्य। एक पूर्णांक, रिटर्न Trueया के रूप में इनपुट लेता है False

Sort[x=Sign@-Differences[y=IntegerDigits@#]]==x&&y[[1]]==Last@y&

स्पष्टीकरण:

पहला खंड "पहाड़ीपन" की जाँच करता है:

  • IntegerDigits: पूर्णांक से अंक प्राप्त करें। में स्टोर करें y
  • -Differences: क्रमिक अंतर और फ्लिप संकेत लें।
  • Sign: प्रत्येक प्रविष्टि को +1 के साथ बदलें यदि सकारात्मक, 0 यदि शून्य हो, और -1 नकारात्मक हो तो। में स्टोर करें x
  • Sort: सबसे छोटी से सबसे बड़ी तक +1, 0, -1 की सूची। में मूल सूची की तुलना करें x

दूसरा खंड यह जाँचता है कि पहले और अंतिम अंक बराबर हैं या नहीं।

इस कोड को परिष्कृत करने की युक्तियों के लिए @IanMiller को टोपी की एक टिप।


तथ्य यह है कि IntegerDigitsऔर Differencesलंबे समय तक कार्य कर रहे हैं नाम थोड़ा परेशान है।
माइकल सीफ़र्ट

निम्नलिखित परिवर्तनों के साथ 5 बाइट्स बचा सकते हैं:Sort[x=Sign@-Differences[y=IntegerDigits@#]]==x&&y[[1]]==Last@y&
इयान मिलर


0

रेटिना 0.8.2 , 52 बाइट्स

.
$*1;$&$*1,
(1+),\1
,
^(1+);(,1+;)*(,;)*(1+,;)*\1,$

इसे ऑनलाइन आज़माएं! लिंक में परीक्षण के मामले शामिल हैं। स्पष्टीकरण:

.
$*1;$&$*1,

प्रत्येक अंक को दो बार, यू द्वारा अलग किया गया ;और ,एस द्वारा समाप्त किया गया । हालाँकि, फिर आप परिणाम को पहले अंक के रूप में सोच सकते हैं ;, फिर, आसन्न अंकों के सभी जोड़े, प्रत्येक जोड़ी के अंकों को अलग किया ,और जोड़े को ;s से अलग किया , फिर एक और ;, फिर अंतिम अंक, फिर एक अंतिम ,

(1+),\1
,

आसन्न अंकों के जोड़े घटाएं। यह छोड़ देता है;,; समान अंकों के लिए और 1असमान अंकों के लिए अधिक से अधिक तरफ होता है। (यह निम्नलिखित रेगेक्स के हिस्से के रूप में किया जा सकता है, लेकिन जाहिर है कि इतना गोल्फ नहीं होगा।)

^(1+);(,1+;)*(,;)*(1+,;)*\1,$

पहले अंक, फिर आरोही अंकों के जोड़े की संख्या, फिर बराबर अंकों के जोड़े की संख्या, फिर अवरोही अंकों की जोड़ी के किसी भी संख्या, फिर पहले अंक को बहुत अंत में फिर से मिलाएं।


0

लाल , 181 बाइट्स

func[n][m: last sort copy t: s: form n
parse t[opt[copy a to m(a: sort unique a)]copy b thru any m
opt[copy c to end(c: sort/reverse unique c)]](s = rejoin[a b c])and(s/1 = last s)]

इसे ऑनलाइन आज़माएं!

अधिक पठनीय:

f: func[n][
    t: s: form n                                    
    m: last sort copy t                             
    parse t [ opt [ copy a to m (a: sort unique a) ] 
              copy b thru any m
              opt [ copy c to end (c: sort/reverse unique c) ]
            ]
    (s = rejoin [ a b c ]) and (s/1 = last s)
]

0

पॉवरशेल, 77 बाइट्स

($x=-join("$($args|%{"-$_;$_"})"|iex))-match'^(-\d)+0*\d+$'-and$x[1]-eq$x[-1]

कम गोल्फ परीक्षण स्क्रिप्ट:

$f = {
                                           # $args = 1,2,3,3,3,2,1
$a=$args|%{"-$_;$_"}                       # "-1;1","-2;2","-3;3","-3;3","-3;3","-2;2","-1;1"
$d="$a"                                    # "-1;1 -2;2 -3;3 -3;3 -3;3 -2;2 -1;1"
$x=-join($d|Invoke-Expression)             # "-1-1-100111"
$x-match'^(-\d)+0*\d+$'-and$x[1]-eq$x[-1]  # $true or $false

}

@(
    ,($True , 1,2,3,2,1 )
    ,($True , 1,2,3,3,3,2,1 )
    ,($True , 9,9 )
    ,($True , 3 )
    ,($True , 2,3,4,5,6,7,9,9,2 )
    ,($False, 1,2,3,2 )
    ,($False, 7,7,8,8,9,6 )
    ,($False, 2,3,2,3,2 )
    ,($False, 4,5,5,6,6,5,5,4 )
    ,($False, 5,6,4,5 )
) | % {
    $expected,$a = $_
    $result = &$f @a
    "$($result-eq$expected): $result"
}

आउटपुट:

True: True
True: True
True: True
True: True
True: True
True: False
True: False
True: False
True: False
True: False

0

सी # (विजुअल सी # इंटरएक्टिव कंपाइलर) , 161 बाइट्स

s=>{var m=s.OrderBy(c=>c).Last();return s[0]==s.Last()&Enumerable.Range(1,s.Length-1).All(i=>i>s.LastIndexOf(m)?s[i-1]>s[i]:i>s.IndexOf(m)?m==s[i]:s[i-1]<s[i]);}

इसे ऑनलाइन आज़माएं!

यहां बताया गया है कि यह कैसे काम करता है ...

  1. इनपुट एक के रूप में है string
  2. सबसे बड़ा अंक ज्ञात कीजिए
  3. सुनिश्चित करें कि पहले और अंतिम अंक समान हैं
  4. सबसे बड़े अंक की अंतिम घटना घटने के बाद अंकों को सुनिश्चित करें
  5. सबसे बड़े अंक की पहली और अंतिम घटना के बीच अंक सुनिश्चित करना सबसे बड़े अंक के बराबर है
  6. सबसे बड़े अंक की पहली घटना बढ़ने से पहले अंकों को सुनिश्चित करें

0

पायथन 3 , 114 बाइट्स

def f(r):
 l=[*r]
 for i in-1,0:
  while 1<len(l)and l[i]<l[(1,-2)[i]]:l.pop(i)
 return 2>len({*l})and r[0]==r[-1]

इसे ऑनलाइन आज़माएं!

कुछ पायथन 2 समाधानों की तुलना में लंबा है, लेकिन यह एक डीफ-आधारित है और मुझे यह पसंद है।


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