সন্ধান করুন এবং ঘোরান


30

কাজটি

এটি একটি সহজ চ্যালেঞ্জ। আপনার ইনপুট একটি একক খালি নয় এমন স্ট্রিং, শুধুমাত্র সংখ্যা ধারণকারী হয় 0123456789এবং হ্যাশ #। এটিতে অঙ্কের ঠিক এক রান থাকবে, যা একটি nonnegative পূর্ণসংখ্যা এনকোড করে এবং স্ট্রিংয়ের শেষে এবং কমপক্ষে একটি মোড়তে পারে #। পূর্ণসংখ্যার শীর্ষস্থানীয় জিরো থাকতে পারে। উদাহরণস্বরূপ, ##44##, 013####এবং 23###1বৈধ ইনপুট তৈরী করছেন, ###, 0099এবং #4#4হয় না।

আপনার কাজটি nস্ট্রিং থেকে পূর্ণসংখ্যা বের করা এবং স্ট্রিংটি ঘোরানো nপদক্ষেপগুলি ডানদিকে আউটপুট করা ।

উদাহরণ

  • ইনপুটটি #1##ডানদিকে 1 ধাপে ঘোরানো উচিত, সুতরাং সঠিক আউটপুট ##1#
  • ইনপুটটি #026###ডানদিকে 26 ধাপে ঘোরানো উচিত, যেহেতু নেতৃস্থানীয় 0 উপেক্ষা করা হচ্ছে। সঠিক আউটপুট হয় 26####0
  • ইনপুট 1####2তাই এটি ডানদিকে 21 ধাপ আবর্তিত করা পূর্ণসংখ্যা 21 শেষ ওভার আবৃত রয়েছে। সঠিক আউটপুট হয় ##21##

বিধি এবং স্কোরিং

আপনি একটি সম্পূর্ণ প্রোগ্রাম বা একটি ফাংশন লিখতে পারেন। সর্বনিম্ন বাইট গণনা জয়, এবং মান লুফোলগুলি অনুমোদিত নয়।

আপনি ধরে নিতে পারেন যে নম্বরটি আপনার ভাষার nস্ট্যান্ডার্ড intধরণের সাথে খাপ খায় । বিপরীতভাবে, যদি সেই আদর্শ intধরণটি স্বেচ্ছাচারিতা-নির্ভুলতা পূর্ণসংখ্যার প্রয়োগ করে, আপনাকে অবশ্যই (তত্ত্বের ভিত্তিতে) একটি নির্বিচারে বৃহতকে সমর্থন করতে হবে n

পরীক্ষার মামলা

#1## -> ##1#
##4## -> #4###
1####1 -> ####11
1####2 -> ##21##
#026### -> 26####0
#000### -> #000###
###82399 -> ##82399#
51379#97 -> #9751379
#98##### -> ###98###
#######4## -> #4########
60752#1183 -> 8360752#11
####99366800## -> 366800######99
########9##### -> ###9##########
91#####515694837 -> 1#####5156948379
###6114558###### -> #6114558########
######219088736090042#### -> 9088736090042##########21
#46055080150577874656291186550000138168########### -> 0138168############4605508015057787465629118655000
568375993099127531613012513406622393034741346840434468680494753262730615610086255892915828812820699971764142551702608639695081452206500085233149468399533981039485419872101852######################3680 -> 99533981039485419872101852######################36805683759930991275316130125134066223930347413468404344686804947532627306156100862558929158288128206999717641425517026086396950814522065000852331494683

7
আমাদের কি সব পরীক্ষার ক্ষেত্রে সমর্থন করতে হবে? এই সংখ্যাগুলির কয়েকটি বেশ বড় ... 8-বিট পূর্ণসংখ্যার ভাষা ব্যবহার করা কি গ্রহণযোগ্য হবে?
ডেনিস

@ ডেনিস ম্যানুয়ালটিতে পূর্ণসংখ্যাটি লোড না করে মডুলার গাণিতিক দ্বারা চ্যালেঞ্জটি সমাধান করা সম্ভব ... তবে আপনি ঠিক বলেছেন, এটি অনেক ভাষায় ঝামেলা। আসুন ধরা যাক আপনার কেবল সেই পরীক্ষাগুলি পরিচালনা করতে হবে যেখানে আপনার ভাষার nস্থানীয় intধরণের (যা স্বেচ্ছাসেবী-নির্ভুলতা হতে পারে) ফিট করে। আমি পরে চ্যালেঞ্জ পাঠ্য আপডেট করব।
Zgarb

ইনপুট = হলে আমাদের কী করা উচিত 1234?
ক্যালকুলেটরফলাইন

2
@ ক্যাটস আরেফ্লুফি "এবং কমপক্ষে একটি #"
ফ্রাইআম দ্য এজিগম্যান

উত্তর:


10

সিজেম, 11 বাইট

q_'#%W%sim>

এটি অনলাইন চেষ্টা করুন! বা সমস্ত পরীক্ষার কেস যাচাই করুন

নোট করুন যে এটি শেষ দুটি পরীক্ষার ক্ষেত্রে কাজ করবে না, কারণ জড়িত নম্বরগুলি 64 বিটে ফিট করে না।

কিভাবে এটা কাজ করে

q_          e# Read all input and push it twice.
  '#%       e# Split at runs of '#'.
     W%     e# Reverse the resulting array.
       si   e# Cast to string, then to int.
         m> e# Rotate the original input that many places to the right.

Oooh... so simple!
Luis Mendo

7

Julia, 71 65 bytes

s->join(circshift([s...],maximum(parse,split(s*s,"#",keep=1<0))))

This is an anonymous function that accepts a string and returns a string. To call it, assign it to a variable.

We append the input to itself, split it into an array with # as the separator, parse each integer, and take the maximum. This defines the number of times we shift the string to the right. We splat the string into a Char array, shift, and join it back together.


7

Python, 66 bytes

lambda l:(2*l)[-int(''.join(l.split('#')[::-1]))%len(l):][:len(l)]

5

Retina, 65 57 49

(\d*)#*(\d+)
$2$1 $0
^\d+
$*
+`1 (.*)(.)
 $2$1
<space>

Saved 8 bytes thanks to Martin!

Try it Online!

Note that this will time out / run out of memory for the very large test cases online, and on most sane machines, for some of the larger ones.

This takes the last number in the string and the first or no number in the string and puts them in front of the string. Then it converts that combined number to unary and repeatedly rotates while dropping a unary digit.


3

Jelly, 12 10 bytes

ẋ2~ṣ0‘ḌṂṙ@

Try it online! or verify all test cases.

Background

Say the input is 51379#97.

By repeating the string twice (51379#9751379#97), we can make sure that it will contain a contiguous representation of the number.

Next, we apply bitwise NOT to all characters. This attempts to cast to int, so '1' gets evaluated to 1, then mapped to ~1 = -2. On failure (#), it returns 0.

For our example, this gives

[-6, -2, -4, -8, -10, 0, -10, -8, -6, -2, -4, -8, -10, 0, -10, -8]

Next, we split at zeroes to separate the part that encodes the number from the rest.

[[-6, -2, -4, -8, -10], [-10, -8, -6, -2, -4, -8, -10], [-10, -8]]

Bitwise NOT maps n to -n - 1, so we increment each to obtain -n.

[[-5, -1, -3, -7, -9], [-9, -7, -5, -1, -3, -7, -9], [-9, -7]]

Next, we convert each list from base 10 to integer.

[-51379, -9751379, -97]

The lowest number is the negative of the one we're searching for. Since Jelly list rotation atom rotates to the left, this avoid multiplying by -1 to rotate to the right.

How it works

ẋ2~ṣ0‘ḌṂṙ@  Main link. Input: S (string)

ẋ2          Repeat the string twice.
  ~         Apply bitwise NOT to all characters.
            This maps 'n' to ~n = -(n+1) and '# to 0.
   ṣ0       Split at occurrences of zeroes.
     ‘      Increment all single-digit numbers.
      Ḍ     Convert each list from base 10 to integer.
       Ṃ    Take the minimum.
        ṙ@  Rotate S that many places to the left.

3

MATL, 28 25 17 16 bytes

!G1Y4XXPZcXvUYS!

8 bytes less borrowing Dennis' idea of splitting the array and reversing the order of the pieces

The two last test cases don't work because the number is too large.

EDIT (May 20, 2016) The code in the link uses Xz instead of Xv, owing to recent changes in the language.

Try it online!

!         % take implicit input: string. Transpose into column char array
G         % push input string again
1Y4       % predefined literal '\d' (for regexp)
XX        % match regexp. Gives cell array with 1 or 2 strings
P         % flip that array
ZcXv      % join the strings in that array, without spaces
U         % convert to number
YS        % rotate the transposed input that many times
!         % put back into row form (string). Implicitly display

2

PowerShell, 153 bytes

(But see the Extra Credit section, below)

param($a)$d=[System.collections.arraylist][char[]]$a;for($b=+("$a$a"-split"#"-ne'')[1];$b;$b--){$r=$d[-1];$d.removeAt($d.Count-1);$d.insert(0,$r)}-join$d

PowerShell doesn't have the concept of "shifting" an array, so I had to roll my own solution. Will take a long time for larger numbers, but it should eventually complete anything that fits in a 32-bit int.

Takes input $a, and sets a new variable $d as a [System.Collections.ArrayList] object. This is done because, technically, arrays in PowerShell are immutable (further explained below in Extra Credit), and thus don't support arbitrary insertions or removals, which is needed for shifting. Then, we enter a for loop.

The initial condition is a trick I found -- if we concatenate the input together, split on #, and ignore empties, the second element of the resulting array will be equal to our number, regardless of wrapping. We set that to $b, and decrement $b each time until it's zero.

Each iteration, we set helper $r as the last element in the arraylist, remove that last element, and then insert the element onto the front ... effectively "shifting" the array to the right by one element.

Finally, we simply output with -join$d so that it's concatenated into one string.


Extra Credit

If the problem was shifting the array left instead of right, we can do it significantly shorter using multiple assignment. Basically, "If the assignment value contains more elements than variables, all the remaining values are assigned to the last variable."

In essence, this means something like $c=@(1,2,3) and $a,$b=$c
will have $a=1 an int and $b=@(2,3) an array.

PowerShell, 90 bytes, does a left shift instead of a right shift

param($a)$b=+("$a$a"-split"#"-ne'')[1];$a=[char[]]$a;for(;$b;$b--){$r,$a=$a;$a+=$r}-join$a

Here we once again take input, and set $b as above. We re-cast $a as a char-array, and then enter the same for loop as above. This time, though, we've not needed to support arbitrary removal/insertion, so we don't need to use the costly [System.Collections.ArrayList] object, nor the expensive method calls. Instead we simply set $r to be the first element of $a, and the remaining elements are re-saved in $a. Then we += to tack it back on to the end.

(As I said, PowerShell arrays are technically immutable, but the += operator here is overloaded - it takes an array and another object, mushes them together (technical term) into a new array, returns that and saves it as the variable name, and destroys the original array. Functionally, we've just added an element to the end of the array, but technically (and from a memory/garbage-cleanup perspective, etc.) it's a brand-new array. This can obviously become a costly operation if the array is large or complex. The flipside is that, since arrays are immutable, indexing into them or iterating over them is very cheap.)

Output remains the same action, with a -join statement to turn it into a single string.


1

Seriously, 21 bytes

,;;+'#@s`≈`MM@#@`/`nΣ

Try it online!

Warning: this solution is very inefficient, so the larger test cases will time out on TIO. Use the local interpreter.

Explanation:

,;;+'#@s`≈`MM@#@`/`nΣ
,;;+                   make 3 copies of input, and concatenate two of them
    '#@s               split on #s
        `≈`MM          convert strings to ints, take maximum
             @#@       explode final copy of input
                `/`n   rotate to the right n times
                    Σ  join

Concat and take maximum: great idea!
Luis Mendo

@LuisMendo I was amused to see Alex's answer pop up with the same strategy while I was writing up the explanation here.
Mego

Looks like the only one that initially used the naïve approach was me :-) (rotating the initial string until all digits were contiguous)
Luis Mendo

1

Mathematica, 69 Bytes

#~StringRotateRight~ToExpression[""<>Reverse@TextCases[#,"Number"]]&

Find sequences of numbers in the, if there are 2 then their order needs to be reversed. Concatenate the strings (if it's only one it just returns the number string). Convert string to numeric and rotate the string that number of times.


FromDigits works instead of ToExpression.
CalculatorFeline

1

Pyth, 22 14 bytes

.>zs-.<zxz\#\#

Try it here!

Explanation

.>zs-.<zxz\#\#    # z=input

     .<z          # rotate z left by
        xz\#      # index of the first occurence of a hash
                  # this ensures that the integer is not wrapped around the end
    -       \#    # filter all hashes out
   s              # cast to an integer, also removes leading zeroes
.>z               # do the final roation of the input string and print it

This works for all testcases and also almosts finishes instantly for the very big numbers.


You can do -...\# instead of h:..."\d+"1. Also, no need to convert z into a list of chars, .> works also on a string.
Jakube

@Jakube Thanks for the hint, was pretty tired when I did this. ^^
Denker

1

JavaScript (ES6) 66

For once, the stupid negative % of javascript for negative numbers come useful

z=>(z+z).substr(-(l=z.length,[a,b]=z.match(/\d+/g),b?b+a:a)%l-l,l)

1
@WashingtonGuedes no, the sum in b+a is a string concatenation. a='32',b='1', (b?b+a:a)=='132', (b|0+a)==33
edc65


1

JavaScript (ES6), 67 64 bytes

s=>(l=s.length,s+s).substr(l-s.split(/#+/).reverse().join``%l,l)

Another port of Dennis's CJam answer.

Edit: Saved 3 bytes by appropriating the part of edc65's answer that he didn't draw attention to.


Using a ternary and a sum instead of reverse().join(), you should beat my score
edc65

@Downgoat Sorry, I've got them mostly right lately but I did this one late at night and so I wasn't thinking straight.
Neil

@edc65 No, that made my score higher. So I copied the s+s trick instead. (I actually thought about that last night but I was too tired to try it out at the time.)
Neil

1

Perl 5, 41 bytes

39 bytes plus two for the -lF flags (-M5.01 is free): perl -lF -M5.01 script.pl

/#+/;map{unshift@F,pop@F}1..$'.$`;say@F

Explanation:

  • -lF reads the input, removes the trailing newline, puts the remainder into the string $_, splits it up into characters, and puts that split into the array @F.
  • /#+/ finds the first string of #s in $_ and sets $` equal to the stuff before it and $' equal to the stuff after it. If $` is empty then $' may contain more #s. However, $'.$` is a string whose initial substring is the number of times to rotate the array.
  • Now we build the list 1..$'.$`, which treats $'.$` as an integer and thus numifies it, which strips any final #s, so the list is from 1 to the number of times to rotate the array.
  • For each element in that list, we rotate the array (pop the last element and unshift it onto the beginning).
  • Then say all the elements of the rotated array.

1

Ruby - 68 72 70 bytes

s=ARGV[0]
p s.split(//).rotate(-(s+s).scan(/\d+/).map(&:to_i).max)*""
  • split converts string into an array
  • (s+s).scan(/\d+/) concatenate string to itself and get an array of numbers (as strings)
  • map(&:to_i) convert strings to ints
  • max pick the largest int
  • rotate max times
  • *"" convert the array back into a string (shorthand for join)

Usage : ruby scriptname.rb "[string]"


i'm new here. what's the etiquette on posting multiple answers in different languages? I added a separate answer in case one was wrong. if it's not ok to add multiple answers, let me know and i'll take it down
FuzzyTree

1
Multiple answers in different languages is fine, even encouraged (provided that they are all correct).
Zgarb

0

05AB1E, 14 13 bytes

Well, the code is very unlikely to terminate for the numbers bigger than 100000, but if you're patient enough, there will be an output :). Code:

'#¡rJ¹sF¤rS\J

Explanation:

'#¡             # Split the input on '#'
   r            # Reverse the stack
    J           # Join the stack
     ¹          # Take the first input
      s         # Swap with the number
       F        # For N in range(0, number), do...
        ¤       #   Obtain the last character
         r      #   Reverse the stack
          S     #   Split everything to individual characters
           \    #   Delete the last character
            J   #   Join the stack

Try it online!

Uses CP-1252 encoding


0

VBSCRIPT, 82 99 BYTES

previous code didn't handle cases with number wrapped over the end

b=len(a):f=replace(a,"#","/",1,1):c=replace(split(f&f,"/")(1),"#",d) mod b:d=right(a,c)&left(a,b-c)

UNGOLFED

b=len(a)                                 -a->implicit input, get its length 
f=replace(a,"#","/",1,1)  -replace first instance of # so we can split later
c=replace(split(f&f,"/")(1),"#",d) mod b    -get the number and calc the mod
d=right(a,c)&left(a,b-c)                    -d->implicit output

this kinda sucks ... there's probably a better way to do it, even in VBscript


Welcome to Programming Puzzles and Code Golf Stack Exchange. This answer could be improved by adding a code breakdown and explanation below your golfed code. Also, could you save bytes by creating a function instead of a program, where a is the function input, and it returns the output? That way, you wouldn't need the inputbox and msgbox calls.
wizzwizz4

Why do you need b?
CalculatorFeline

0

Mathematica, 73 58 bytes

#~StringRotateRight~Max[FromDigits/@StringSplit[#<>#,"#"]]&

Much byte. 15 bytes saved thanks to IPoiler


StringRotateRight saves some bytes here.
IPoiler

0

Matlab(73)

  @(a)regexprep(a,'(\d*)#*(\d*)#*','${circshift($0,[0 str2num([$2 $1])])}')
  • This is using another approach that i wonder if @luis used it, because refering to his description there is some points in common while (un)?fortunately i dont understand the cropped Matl language.

0

matlab(86)72

 @(n)circshift(n,[0 str2num(circshift(n(n~='#'),[0,-find(n=='#',1)+1]))])
  • The function totates the string two times, once for integer extraction, second for the desired task, it doesnt take too much time because matlab proceeds to rotate by (Dim)modulus(Length) to the exception that it falls in segmentation failure for bigger ranges.

  • Will struggle how to golf it more ....


(86)

  @(n)circshift(n,[0 str2num([strtok(n(find(n=='#',1,'last'):end),'#') strtok(n,'#')])])
  • The difference between this function and the previous, this one does concatenate two distant occurences of integers way backwards, while the first one just rotates it.
আমাদের সাইট ব্যবহার করে, আপনি স্বীকার করেছেন যে আপনি আমাদের কুকি নীতি এবং গোপনীয়তা নীতিটি পড়েছেন এবং বুঝতে পেরেছেন ।
Licensed under cc by-sa 3.0 with attribution required.