Pendapatan Variabel-Harga!


16

Pengantar dan kredit

Anggaplah Anda seorang bartender. Anda memiliki banyak orang yang bahagia di bar Anda paling sering, tetapi banyak yang hanya minum minuman yang sama dan terlalu sedikit untuk selera Anda dan Anda ingin mengubahnya. Jadi Anda memperkenalkan sistem di mana harga minuman bervariasi, tergantung pada berapa banyak yang telah terjual, tetapi tidak pernah lebih atau lebih murah daripada ambang batas tertentu. Untuk beberapa alasan aneh Anda selalu lupa untuk melacak dengan benar semua minuman yang dijual dan harga dan dengan demikian Anda perlu memikirkan sepotong kode pendek (= mudah diingat!) Yang menghitung untuk Anda mengingat jumlah minuman yang dikonsumsi.

Tantangan ini telah muncul dalam ujian tengah semester pada tahun 2012 di kursus pemrograman fungsional di uni saya dan profesor saya boleh saja mempostingnya di sini. Kami telah memberikan contoh solusi dalam bahasa ujian.

Memasukkan

Masukan Anda akan menjadi daftar string yang tidak mengandung spasi - ini adalah nama minuman yang dijual. Ambil input menggunakan metode input yang disukai dan diterima secara umum.

Keluaran

Output Anda akan menjadi satu nomor - ini adalah pendapatan yang Anda hasilkan malam ini. Berikan output menggunakan pilihan Anda, metode output diterima secara umum.

Apa yang harus dilakukan?

Ini berlaku untuk setiap minuman secara individual:

  • Harga awal adalah 10.
  • Setiap kali minuman dibeli, harganya ditabrak oleh 1 untuk pembeli berikutnya.
  • Harga maksimal adalah 50. Jika minuman telah dibeli seharga 50, harga baru akan menjadi 10 lagi.

Tugas Anda adalah menemukan pendapatan keseluruhan, yang dihasilkan oleh daftar input minuman mengingat aturan di atas.


Jika Anda bertanya-tanya: "50 dolar benar-benar mahal untuk minum!", Ini adalah 50-deci Bucks, jadi 50 * 0,1 * Unit, tapi saya memilih untuk menggunakan 10-50 untuk tidak mengecualikan bahasa tanpa aritmatika floating point.

Yang menang?

Ini adalah , jadi kode terpendek dalam byte menang! Aturan standar berlaku.

Kasus Pojok Potensial

Jika daftar input kosong, output harus 0.
Daftar input tidak dapat dianggap disortir berdasarkan minuman.

Contohnya

[] -> 0
["A"] -> 10
["A","B"] -> 20
["A","A","B"] -> 31
["A","B","A"] -> 31
["A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A"] -> 1240
["A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","B","B","B","C","C","D"] -> 1304 
["D","A","A","C","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","B","B","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","B","C"] -> 1304
["A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","B","B","B","C","C","D","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A"] -> 1304

1
Alat peraga menanyakan profesor Anda sebelum memposting, cukup langkah OG.
Magic Gurita Guci

Jawaban:


4

JavaScript (ES6), 50 byte

a=>a.map(x=>t+=d[x]=d[x]<50?d[x]+1:10,t=0,d={})&&t

Di mana Anda init d[x]ke 10?
Titus

@Titus Jika d[x]belum diatur, itu undefined; ini membuat d[x]<50return false, jadi d[x]=d[x]<50?d[x]+1:10set d[x]to 10.
ETHproductions

Saya lupa bahwa JS memiliki undefined. :)
Titus

4

Python 2, 79 74 54 48 Bytes

Peningkatan jumlah byte masif dengan memikirkan kembali masalah. Saya ingin menyingkirkan para intpemain tetapi otak saya tidak berfungsi . Memanfaatkan l.pop()untuk menghindari pemangkasan daftar dua kali dan beberapa rekursi lambda yang baik :)

f=lambda l:l and l.count(l.pop())%41+10+f(l)or 0

terima kasih kepada Jonathan Allan untuk menghemat 6 byte :)

Versi 54-byte lama saya cukup saya banggakan :)

f=lambda l:int(l>[])and~-l.count(l[0])%41+10+f(l[1:])

...l>[]and 1*~...untuk menyimpan 3 byte yang Anda tahu Anda bisa.
Jonathan Allan

Bahkan 1 kurang dengan:f=lambda l:l and~-l.count(l[0])%41+10+f(l[1:])or 0
Jonathan Allan

Oooh dan dua lainnya dengan:f=lambda l:l and l.count(l.pop())%41+10+f(l)or 0
Jonathan Allan

@ Jonathan Allan, terima kasih atas tipsnya! Saya akan segera memperbarui posting saya :)
Kade

2

Pyth, 15 byte

ssm<*lQ}T50/Qd{

Program yang mengambil input daftar dan mencetak hasilnya.

Test suite (Baris pertama untuk memungkinkan beberapa input)

Bagaimana itu bekerja

ssm<*lQ}T50/Qd{   Program. Input: Q
ssm<*lQ}T50/Qd{Q  Implicit input fill
              {Q  Deduplicate Q
  m               Map over that with variable d:
       }T50        Yield [10, 11, 12, ..., 48, 49, 50]
    *lQ            Repeat len(Q) times
   <       /Qd     First Q.count(d) elements of that
 s                Flatten
s                 Sum
                  Implicitly print

2

Jelly , 14 11 10 byte

50⁵rṁЀĠSS

TryItOnline!

Bagaimana?

50⁵rṁЀĠSS - Main link: list of drink names                e.g. ['d', 'a', 'b', 'a', 'c']
       Ġ   - group indices by values                       e.g. [[2, 4], [3], [5], [1]]
  ⁵        - 10
50         - 50
   r       - inclusive range, i.e. [10, 11, 12, ..., 48, 49, 50]
    ṁЀ    - mould left (the range) like €ach of right(Ð)  e.g. [[10, 11], [10], [10], [10]]
                 note: moulding wraps, so 42 items becomes [10, 11, 12, ..., 48, 49, 50, 10]
        S  - sum (vectorises)                              e.g. [40, 11]
         S - sum                                           e.g. 51

2

05AB1E , 16 15 byte

Terima kasih kepada Emigna karena telah menghemat satu byte!

ÎÙv¹y¢L<41%T+OO

Menggunakan pengkodean CP-1252 . Cobalah online!


ÎÙv¹y¢L<41%T+OOharus bekerja selama 1 byte disimpan
Emigna

@Emigna Itu bagus! Terima kasih :)
Adnan

1

Perl 41 Bytes

Termasuk +1 untuk -p

$\+=$H{$_}+=$H{$_}?$H{$_}>49?-40:1:10;}{

Mengambil input pada baris baru.

Menambahkan nilai hash dengan: 10 jika itu undef, -40apakah itu > 49yaitu 50, atau 1sebaliknya. Ini kemudian ditambahkan ke $\, pemisah keluaran, yang-p dicetak.

Contoh:

$ echo -e 'A\nB\nA' | perl -pe '$\+=$H{$_}+=$H{$_}?$H{$_}>49?-40:1:10;}{'
31

1

05AB1E , 13 byte

{.¡€gL<41%T+O

Penjelasan

["A","B","A"] digunakan sebagai contoh.

{               # sort input
                # STACK: ["A","A","B"]
 .¡             # split on different
                # STACK: [["A","A"],["B"]]
   €g           # length of each sublist
                # STACK: [2,1]
     L          # range [1 ... x] (vectorized)
                # STACK: [1,2,1]
      <         # decrease by 1
                # STACK: [0,1,0]
       41%      # mod 41
                # STACK: [0,1,0]
          T+    # add 10
                # STACK: [10,11,10]
            O   # sum
                # OUTPUT: 31

1

C ++ 14, 105 byte

Sebagai lambda tanpa nama generik kembali melalui parameter referensi. Membutuhkan input untuk menjadi wadah stringyang dimiliki push_back, seperti vector<string>.

Menggunakan %41+10trik dari jawaban Python Kade .

[](auto X,int&r){r=0;decltype(X)P;for(auto x:X){int d=0;for(auto p:P)d+=x==p;r+=d%41+10;P.push_back(x);}}

Membuat wadah kosong Psebagai memori yang sudah dilayani. Harga dihitung dengan menghitung xmasukP .

Tidak digabungkan dan digunakan:

#include<iostream>
#include<vector>
#include<string>

using namespace std;

auto f=
[](auto X, int& r){
  r = 0;
  decltype(X) P;
  for (auto x:X){
    int d = 0;
    for (auto p:P)
      d += x==p;
    r += d % 41 + 10;
    P.push_back(x);
  }
}
;

int main(){
 int r;
 vector<string> V;
 f(V,r);cout << r << endl;
 V={"A"};
 f(V,r);cout << r << endl;
 V={"A","B"};
 f(V,r);cout << r << endl;
 V={"A","B","C"};
 f(V,r);cout << r << endl;
 V={"A","A"};
 f(V,r);cout << r << endl;
 V={"A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A"};
 f(V,r);cout << r << endl;
 V={"A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","B","B","B","C","C","D"};
 f(V,r);cout << r << endl;
 V={"A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","B","C"};
 f(V,r);cout << r << endl;
}

0

Mathematica, 64 byte

Terasa seperti itu harus lebih pendek.

Tr[(19+#)#/2&/@(Length/@Gather@#//.z_/;z>41:>Sequence[41,z-41])]&

Length/@Gather@#menghitung pengulangan setiap minuman. //.z_/;z>41:>Sequence[41,z-41]membagi setiap bilangan bulat yang zmelebihi 41 dalam ini menjadi 41dan z-41, untuk mencerminkan penurunan harga. Kemudian masing-masing penghitungan dicolokkan ke dalam formula (19+#)#/2, yang merupakan total biaya #minuman #paling lama 41. Akhirnya, Trjumlahkan semua biaya itu.


0

k, 22 byte

Argumen dapat berupa daftar apa saja - string, angka, dll.

{+/,/10+(#:'=x)#\:!41}

The qterjemahan lebih mudah dibaca:

{sum raze 10+(count each group x)#\:til 41}

0

C #, 193 byte + 33

Tambahan 33 byte untuk using System.Collections.Generic;

void m(string[]a){int t=0;Dictionary<string,int>v=new Dictionary<string,int>();foreach(string s in a){if(v.ContainsKey(s)){v[s]=v[s]==50?10:v[s]+1;}else{v.Add(s,10);}t+=v[s];}Console.WriteLine(t);}

Saya yakin ini bisa dilupakan untuk bermain golf, Kamus jelas bukan cara terbaik untuk melakukan ini dan saya mungkin bisa bekerja ternary ke if saya. Selain itu saya pikir tidak apa-apa!

Contoh:

a = {"A", "A", "A", "B", "B", "C"};
//output = 64

a = {"A", "A" ,"A", "A" ,"A", "A" ,"A", "A" ,"A", "A" ,"A", "A" ,"A", "A" ,"A", "A" ,"A", "A" ,"A", "A" ,"A", "A" ,"A", "A" ,"A", "A" ,"A", "A" ,"A", "A" ,"A", "A" ,"A", "A" ,"A", "A" ,"A", "A" ,"A", "A" ,"A", "A" ,"A", "A" ,"A", "A" ,"A", "A" ,"A", "A" ,"A", "A" ,"A",, "A" ,"A", "A" ,"A", "A" ,"A", "A" ,"A", "A" ,"A", "A" ,"A", "A" ,"A", "A" ,"A", "A" ,"A", "A" ,"A", "A" ,"A", "A" ,"A", "A" ,"A", "A" ,"A" "A" ,"A", "A" ,"A", "B", "B", "C"};
//output 727

Tidak disatukan

void m(string[] a)
{
    int t=0;
    Dictionary<string,int> v = new Dictionary<string,int>();
    foreach(string s in a)
    {
        if(v.ContainsKey(s))
        {
            v[s]=v[s]==50?10:v[s]+1;
        }
        else
        {
            v.Add(s,10);
        }
        t+=v[s];
    }
    Console.Write(t);
}

0

Clojure, 79 byte

#(apply +(mapcat(fn[l](for[i(range l)](+(mod i 41)10)))(vals(frequencies %)))))

Hitung frekuensi minuman, lalu hitung harga dasar sebagai 10 + (i % 41). mapcatmenggabungkannya dan apply +menghitung jumlahnya.


0

PHP, 47 byte

while($k=$argv[++$i])$s+=10+$p[$k]++%41;echo$s;

mengambil input dari argumen baris perintah; jalankan bersama -r.

Dengan menggunakan situs kami, Anda mengakui telah membaca dan memahami Kebijakan Cookie dan Kebijakan Privasi kami.
Licensed under cc by-sa 3.0 with attribution required.