Arrays beginnen bei


10

Ihre Aufgabe ist es, ein Array von Zahlen und eine reelle Zahl zu nehmen und den Wert an dieser Stelle im Array zurückzugeben. Arrays beginnen bei π und werden in π Intervallen gezählt. Die Sache ist, wir werden tatsächlich zwischen Elementen interpolieren, wenn der "Index" gegeben ist. Als Beispiel:

Index:    1π   2π   3π   4π   5π   6π
Array: [ 1.1, 1.3, 6.9, 4.2, 1.3, 3.7 ]

Da es π , müssen wir die obligatorische Trigonometrie durchführen, also verwenden wir die Kosinusinterpolation mit der folgenden Formel:

cos(ichmodπ)+12(α- -β)+β

wo:

  • ich ist die Eingabe "Index"
  • α ist der Wert des Elements unmittelbar vor dem "Index"
  • β ist der Wert des Elements unmittelbar nach dem "Index"
  • cos nimmt seinen Winkel im Bogenmaß

Beispiel

Gegeben [1.3, 3.7, 6.9], 5.3:

Index 5.3 liegt zwischen 1π und 2π , daher wird 1.3 für beforeund 3.7 für verwendet after. Wenn wir es in die Formel einfügen, erhalten wir:

cos(5.3modπ)+12(1.3- -3.7)+3.7

Welches kommt zu 3.165 heraus

Anmerkungen

  • Eingabe und Ausgabe können in jedem geeigneten Format erfolgen
  • Sie können davon ausgehen, dass die Eingabenummer größer als π und kleiner als array length* π
  • Sie können davon ausgehen, dass das Eingabearray mindestens 2 Elemente lang ist.
  • Ihr Ergebnis muss mindestens zwei Dezimalstellen genau sein, auf 0,05 genau sein und Zahlen bis zu 100 für diese Genauigkeit / Genauigkeit unterstützen. (Schwimmer mit einfacher Genauigkeit sind mehr als ausreichend, um diese Anforderung zu erfüllen.)

Viel Spaß beim Golfen!


8
Zu Ihrer Information, es könnte kürzer sein, das Umschreiben als Verwendung der Halbwinkelformel für zu schreiben . cos ( x / 2 ) 2 cos(cos(x)+1)/.2cos(x/.2)2cos
xnor

Kann ich ein Wörterbuch mit doppelten Schlüsseln aufnehmen? Die Doppel werden natürlich ganze Zahlen sein.
Verkörperung der Unwissenheit

@EmbodimentofIgnorance, sicher. Ich bezweifle, dass Ihnen das helfen wird, aber das ist eine absolut vernünftige Darstellung von Arrays, da Lua das so macht.
Beefster

@ KevinCruijssen Ich verstehe nicht, warum das wichtig wäre. 3,7 liegt zwischen pi und 2pi.
Beefster

Antworten:


5

R , 59 53 Bytes

function(x,i)x[0:1+i%/%pi]%*%c(a<-cos(i%%pi/2)^2,1-a)

Probieren Sie es online aus!

Nichts zu klug hier - nur eine R-Version der Formel in der Frage. Vielen Dank an @MickyT für das Speichern eines Bytes und an @Giueseppe und indirekt an @xnor für zwei weitere Byte sowie an @RobinRyder für das Speichern weiterer 3.


Ich denke, Sie können ein Byte mit...*(cos(i%%pi)+1)/2
MickyT

@ MickyT danke, ich hatte ursprünglich die +1 in die Klammern gesetzt, aber ein redundantes Klammerpaar hinzugefügt, so dass am Ende 60 Bytes
Nick Kennedy

56 Bytes nach xnors Kommentar zur Halbwinkelformel.
Giuseppe


4

Python 3.8 (Vorabversion) , 85 74 Byte

-8 Bytes dank @xnor
-2 Bytes dank @Quintec

Dies nutzt den neuen :=Zuweisungsoperator der Python 3.8-Vorabversion . Davon abgesehen ist dies wirklich nur die in Python geschriebene Gleichung.

import math
lambda l,i:cos(i%math.pi/2)**2*(l[(j:=int(i/pi))-1]-l[j])+l[j]

Verwendungszweck:

>>> p=lambda l,i:cos(i%math.pi/2)**2*(l[(j:=int(i/pi))-1]-l[j])+l[j]
>>> print(p([1.3, 3.7, 6.9],5.3))
3.165249203414993

Probieren Sie es online aus!


1
Sie können einfach die erste Stelle zuweisen, jdie erwähnt wird. Ein Teil der Fähigkeit von Zuweisungsausdrücken besteht darin, dass sie den Wert auswerten und ihm zuweisen.
xnor

1
Noch ein Byte speichern: Verwenden Sie Trig-Identitäten, um (cos(i%pi)+1)/2 umcos(i%pi/2)**2
xnor

@xnor Guter Punkt. Ich wusste, dass ich das falsch
benutzte

1
Sie können die p=
löschen,

1
Vergessen,
Bytecount

3

Gelee , 17 Bytes

d©ØPṪÆẠ‘H×I_@Ḋ}®ị

ich

Probieren Sie es online aus!

Wie?

cos(ichmodπ)+12

d©ØPṪÆẠ‘H×I_@Ḋ}®ị - Link: number, i; list of numbers, A
  ØP              - pi (ish) = 3.141592653589793
d                 - divmod = [i//pi, i%pi]
 ©                - (copy to register for later)
    Ṫ             - tail (gets i%pi leaving register copy as [i//pi])  
     ÆẠ           - cosine = cos(i%pi)
       ‘          - increment
        H         - halve
         ×        - multiply by A (vectorises)
          I       - increments -- i.e. (cos(i%pi)+1)(r-l)/2 for neighbours [l,r]
             Ḋ}   - dequeue A
           _@     - swapped arg subtract (vectorises) -- i.e. r-(cos(i%pi)+1)(r-l)/2
                  -                                         = r+(cos(i%pi)+1)(l-r)/2
               ®  - recall value from the register
                ị - index into (vectorises) -- i.e. [β+(cos(i%pi)+1)(α-β)/2]
                  - implicit print of Jelly representation (only 1 entry so [] wont appear)



1

Stax , 17 Bytes

≈ëBü☺ÆssÅ¢â)KjjïΔ

Führen Sie es aus und debuggen Sie es

Ausgepackt, ungolfed und kommentiert sieht es so aus.

VP|%    divmod with pi;  push div and mod results separately
|7^h    do (cos(modpart) + 1) / 2
sX      swap the original div result to top of stack, store it in the x register
v       decrement
;:-     pairwise differences of array
@       get element at index
N*      negate and multiply
;x@     get element from the original array at the x index, where x is the register
+       add

Führen Sie diesen aus



1

APL + WIN, 39 37 Bytes

2 Bytes dank Adám gespeichert

2⊃m+(-/m←⎕[0 1+⌊n÷○1])÷2÷1+2○(○1)|n←⎕

Probieren Sie es online aus! Dyalog Classic

Erläuterung:

n←⎕ prompt for input of integer

2÷1+2○(○1)|n evaluate first term of formula

[0 1+⌊n÷○1] identify indices of alpha and beta

m←⎕[...] prompt for input of vector and select alpha and beta

-/m alpha-beta

2⊃m+ take result of adding beta to complete the equation 



0

Gelee , 23 20 18 Bytes

³%ØPÆẠ×_++H
÷ØPịÇ/

Probieren Sie es online aus!

÷ØPịṁؽµ³%ØPÆẠ×I_@SH    Dyadic link, arguments x (index) and Z (array):
֯P                     x/pi
   ị                    Index (into Z).
                        When x/pi is an integer, returns that elt of Z.
                        Otherwise returns 2 elements at floor and ceiling.
     ؽ                   [1,2] (generic 2 element array)
    ṁؽ                 Mold; shape like [1,2] to ensure we have 2 elements.
       µ                Start a new, monadic chain with the result [a,b]
        ³%ØPÆẠ×I_@SH    Monadic chain
        ³               x
         %ØP            x mod pi
            ÆẠ          Unarccosine; cos(x mod pi).
               I          Increment; b-a.
              ×I        (b-a) cos(x mod pi)
                  S       a+b
                _@S     a + b - (b-a) cos(x mod pi)
                   H    Halve; this is equivalent to our desired result.

0

Attache , 54 Bytes

${Cos[y%PI/2]^2*&`-@(j:=x[1'-1*Floor[y'-y/PI]-1])+j@1}

Probieren Sie es online aus!

Erläuterung

${Cos[y%PI/2]^2*&`-@(j:=x[1'-1*Floor[y'-y/PI]-1])+j@1}
${                                                   }  parameters: x, y
  Cos[y%PI/2]^2                                         the scaling function factor
               *                                        times
                     j:=                                set j to
                        x[                     ]        the element in x at
                          1'-1*Floor[y'-y/PI]-1         the closest indices scaled by PI
                &`-@(                           )       spread subtraction over bounds
                                                 +j@1   add the upper bound

0

C (GCC) 99 79 Bytes

-20 Bytes Deckenkatze

float P=3.141593;b;
#define f(i,a)(cos(fmod(i,P))+1)/2*(a[b=i/P-1]-a[++b])+a[b]

Probieren Sie es online aus!

Code anrufen

int main() {
  float a[3] = {1.3,3.7,6.9};
  printf("%f\n", f(5.3,a));
}

Beachten Sie, dass das Compiler-Flag -lmfür die Verknüpfung mit Mathematikbibliotheken erforderlich ist , also +3 Byte, wenn Sie das zählen.


0

05AB1E , 22 21 20 19 Bytes

žq‰`ž>;UÝèÐÁ-θX*-θ

Probieren Sie es online aus oder überprüfen Sie weitere Testfälle .

Erläuterung:

žq        # Take the divmod PI of the (implicit) input-decimal
           # (part = input integer-divided by PI, remainder = input modulo-PI)
           #  i.e. 5.3 → [1, 2.158...]
   `       # Push both values separately to the stack
    ž     # Take the cosine of the remainder
           #  i.e. 2.158... → -0.554...
      >    # Increase it by 1
           #  i.e. -0.554... → 0.554...
       ;   # Halve it
           #  i.e. 0.554... → 0.222...
        U  # Pop and store it in variable `X`
    Ý      # Pop the part, and push a list in the range [0, part]
           #  i.e. 1 → [0, 1]
     è     # (0-based) index all of them into the (implicit) input-list
           #   i.e. [1.3, 3.7, 6.9] and [0, 1] → [1.3, 3.7]
Ð          # Triplicate this list
 Á         # Rotate the last copy once towards the right
           #  i.e. [1.3, 3.7] → [3.7, 1.3]
  -        # Subtract the values in the top two lists from one another
           #  i.e. [1.3, 3.7] and [3.7, 1.3] → [-2.4, 2.4]
   θ       # Pop and only leave the last value of this list
           #  i.e. [-2.4, 2.4] → 2.4
    X*     # Multiply it by `X`
           #  i.e. 2.4 * `X`=0.222... → 0.534...
     -     # Subtract it from each of the values in the list we triplicated
           #  i.e. [1.3, 3.7] - 0.534... → [0.765..., 3.165...]
      θ    # And only leave the last value of this list
           #  i.e. [0.765..., 3.165...] → 3.165...
           # (which is output implicitly as result)

Durch die Nutzung unserer Website bestätigen Sie, dass Sie unsere Cookie-Richtlinie und Datenschutzrichtlinie gelesen und verstanden haben.
Licensed under cc by-sa 3.0 with attribution required.