From 07753fb745c695871d3b2706d4c00d46422234df Mon Sep 17 00:00:00 2001 From: Bob Brhayan Okoth Date: Wed, 4 Oct 2023 14:53:24 +0300 Subject: [PATCH] translated algorithm fun article --- _content/tour/swa/algorithms-fun.article | 152 +++++++++++------------ 1 file changed, 75 insertions(+), 77 deletions(-) diff --git a/_content/tour/swa/algorithms-fun.article b/_content/tour/swa/algorithms-fun.article index 36ff14e9..1824440f 100644 --- a/_content/tour/swa/algorithms-fun.article +++ b/_content/tour/swa/algorithms-fun.article @@ -1,78 +1,76 @@ -Fun Problems -This section contains a set fun code challenges that showcase some of the capabilities offered by Go. - -* Sleeping Barber Problem - -- [[https://www.ardanlabs.com/training/individual-on-demand/ultimate-go-bundle/][Watch The Video]] -- Need Financial Assistance, Use Our [[https://www.ardanlabs.com/scholarship/][Scholarship Form]] - -This sample program implements the sleeping barber problem. - -- See more at [[https://en.wikipedia.org/wiki/Sleeping_barber_problem][https://en.wikipedia.org/wiki/Sleeping_barber_problem]] - -There is one barber in the barber shop, one barber chair and `n` chairs for -waiting customers. If there are no customers, the barber sits down in the -barber chair and takes a nap. An arriving customer must wake the barber. -Subsequent arriving customers take a waiting chair if any are empty or -leave if all chairs are full. - -*Output:* - - Opening the shop - Barber ready to work - Customer "cust-1" entered shop - Customer "cust-1" takes a seat and waits - Barber servicing customer "cust-1" - Barber finished customer "cust-1" - Barber taking a nap - Customer "cust-2" entered shop - Customer "cust-2" takes a seat and waits - Barber servicing customer "cust-2" - Customer "cust-3" entered shop - Customer "cust-3" takes a seat and waits - Barber finished customer "cust-2" - Barber servicing customer "cust-3" - Customer "cust-4" entered shop - Customer "cust-4" takes a seat and waits - Closing the shop - Barber finished customer "cust-3" - Barber servicing customer "cust-4" - Barber finished customer "cust-4" - Shop closed +Mazoezi ya kufurahisha +Sehemu hii ina mazoezi kadhaa za msimbo wa kufurahisha ambao unaonyesha baadhi ya uwezo unaotolewa na Go. + +* Zoezi la Kinyozi wa Kulala + +- [[https://www.ardanlabs.com/training/individual-on-demand/ultimate-go-bundle/][Tazama video]] +- Ukihitaji msaada wa kifedha, tumia tuvuti [[https://www.ardanlabs.com/scholarship/][Fomu ya udhamini]] + +Programu ya sampuli hii hutekeleza zoezi la kinyozi kulala + +- Tazama mengi katika [[https://en.wikipedia.org/wiki/Sleeping_barber_problem][https://en.wikipedia.org/wiki/Sleeping_barber_problem]] + +Kuna kinyozi mmoja kwenye kinyozi, kiti kimoja cha kinyozi na viti `n` vya +wateja wanaosubiri. Ikiwa hakuna wateja, kinyozi huketi chini +kwenye kiti cha kinyozi na kulala. Wateja wanaowasili baadaye huchukua +kiti cha kungojea ikiwa chochote ni tupu au kuondoka ikiwa viti vyote vimejaa. + +*Pato:* + + Kufungua duka + Kinyozi tayari kufanya kazi + mteja"cust-1" aliingia dukani + mteja "cust-1" aketi na kusubiri + Kinyozi anahudumia mteja "cust-1" + kinyozi amemaliza huduma kwa "cust-1" + Kinyozi analala + Mteja "cust-2" aingia dukani + Mteja "cust-2" aketi na kusubiri + Kinyozi anahudumia mteja "cust-2" + mteja "cust-3" aingia dukani + mteja "cust-3" taketi na kusubiri + kinyozi amaliza huduma kwa "cust-2" + Kinyozi anahudumia mteja "cust-3" + Mteja "cust-4" aingia dukani + Mteja "cust-4" aketi na kusubiri + Duka lafungwa + kinyozi amaliza huduma kwa "cust-3" + Kinyozi amaliza huduma kwa "cust-4" + kinyozi amemaliza kumhudumia mteja "cust-4" + Duka lafungwa .play algorithms/fun/barber.go -* Frequency +* Uzoefu -This sample programs shows you how to implement a function that can find -the frequency of a given rune that is used in a specified sentence. +Programu za hizi sampuli hukuonyesha jinsi ya kutekeleza kazi ambayo inaweza kupata +uzoefu wa rune iliyotolewa ambayo hutumiwa katika sentensi maalum. -- Sequential: A linear algorithm to perform a rune count. -- Concurrent: A concurrent algorithm to perform a rune count. +- Mfululizo: Algorithm ya mstari kutekeleza hesabu ya rune +- Sambamba: Algorithm inayofanana ya kutekeleza hesabu ya rune.. .play algorithms/fun/freq_sequential.go .play algorithms/fun/freq_concurrent.go -* Variable Length Quantity encoding/decoding. +* Urefu Unaobadilika Wingi usimbaji/usimbuaji. -This sample program showcases how Go can be leveraged to implement variable length quantity encoding/decoding. +Mpango huu wa sampuli unaonyesha jinsi Go inaweza kutumiwa ili kutekeleza usimbaji wa wingi wa urefu tofauti. -- See more at [[https://en.wikipedia.org/wiki/Variable-length_code][https://en.wikipedia.org/wiki/Variable-length_code]] +- Mengi kwenye [[https://en.wikipedia.org/wiki/Variable-length_code][https://en.wikipedia.org/wiki/Variable-length_code]] -In short, the goal of this encoding is to save encode integer values in -a way that would save bytes. Only the first 7 bits of each byte is significant -(right-justified; sort of like an ASCII byte). So, if you have a 32-bit value, -you have to unpack it into a series of 7-bit bytes. Of course, you will have -a variable number of bytes depending upon your integer. To indicate which -is the last byte of the series, you leave bit #7 clear. In all of the -preceding bytes, you set bit #7. +Kwa kifupi, lengo la usimbuaji huu ni kuhifadhi nambari kamili kwa njia ambayo ingeokoa baiti. +Biti 7 pekee za kila baiti ndizo muhimu (zimehesabiwa haki; aina kama baiti ya ASCII). +Kwa hivyo, ikiwa una thamani ya baiti 32, unapaswa kuifungua kwenye mfululizo wa baiti 7. +Kwa kweli, utakuwa na idadi tofauti ya ka kulingana na nambari yako kamili. +Ili kuonyesha ni baiti ipi ya mwisho ya mfululizo, unaacha wazi kidogo #7. +Katika baiti zote zilizotangulia, uliweka biti #7. -So, if an integer is between 0-127, it can be represented as one byte. The -largest integer allowed is 0FFFFFFF, which translates to 4 bytes variable -length. Here are examples of delta-times as 32-bit values, and the variable -length quantities that they translate to: +Kwa hivyo, ikiwa nambari kamili iko kati ya 0 127, inaweza kuwakilishwa kama baiti moja. +Nambari kamili kubwa inayoruhusiwa ni 0FFFFFFF, wambayo hutafsiriwa kuwa urefu tofauti +wa baiti 4. Hapa kuna mifano ya nyakati za delta kama maadili biti 32, na idadi ya urefu +tofauti ambayo anatafsiri kwa: - NUMBER VARIABLE QUANTITY + NAMBARI IDADI YA KUTOFAUTIANA 00000000 00 00000040 40 0000007F 7F @@ -86,13 +84,13 @@ length quantities that they translate to: 08000000 C0 80 80 00 0FFFFFFF FF FF FF 7F -A variable-length quantity (VLQ) is a universal code that uses an arbitrary -number of binary octets (eight-bit bytes) to represent an arbitrarily large -integer. It was defined for use in the standard MIDI file format[1] to save -additional space for a resource constrained system, and is also used in the -later Extensible Music Format (XMF). A VLQ is essentially a base-128 -representation of an unsigned integer with the addition of the eighth bit -to mark continuation of bytes. See the example below. +Kiasi cha urefu unaobadilika (VLQ) ni msimbo wa ulimwengu wote unaotumia kiholela. +idadi ya pweza jozi (baiti-biti nane) ili kuwakilisha kubwa kiholela +nambari kamili. Ilifafanuliwa kwa matumizi katika umbizo la kawaida la faili la MIDI[1] kuhifadhi +nafasi ya ziada kwa ajili ya mfumo wa rasilimali vikwazo, na pia kutumika katika +baadaye Muundo wa Muziki Unaoongezwa (XMF). VLQ kimsingi ni msingi-128 +uwakilishi wa nambari kamili ambayo haijatiwa saini na nyongeza ya biti ya nane +kuashiria kuendelea kwa baiti. Tazama mfano hapa chini. Int: 16384 IntHex: 0x00004000 @@ -100,31 +98,31 @@ to mark continuation of bytes. See the example below. VLQHex: 0x81 0x80 0x00 VLQBin: 00000000 10000001 10000000 00000000 -Lets say I want to represent the number 3435 in VLQ. 3435 in -binary is 110101101011. We can not fit this in a byte. So we will -chop it up from the end in 7-bit blocks. +Wacha tuseme ninataka kuwakilisha nambari 3435 katika VLQ. 3435 ndani +binary ni 110101101011. Hatuwezi kutoshea hii kwa baiti. Kwa hivyo tutafanya +kuikata kutoka mwisho katika vitalu 7-bit. Septet 7 6 5 4 3 2 1 #1 1 1 0 1 0 1 1 #2 0 0 1 1 0 1 0 -Now we prepend all but the last with a 1-bit to indicate that an octet -follows and prepend a 0-bit to the last, signalling the final octet. +Sasa tunatanguliza zote isipokuwa za mwisho tukiwa na biti-1 ili kuonyesha kwamba oktet +hufuata na kutayarisha 0-bit hadi ya mwisho, ikiashiria oktet ya mwisho. Octet 8 7 6 5 4 3 2 1 #1 0 1 1 0 1 0 1 1 #2 1 0 0 1 1 0 1 0 -Finally we concatenate them, most significant octet first, into +Hatimaye tunayaunganisha, muhimu zaidi oktet kwanza, ndani -Encoded: 10011010 01101011 ToHex: 0x9A 0x6B +Imesimbwa: 10011010 01101011 ToHex: 0x9A 0x6B -*Extra* *Resources:* +*Ziada* *Rasilimali:* - [[https://en.wikipedia.org/wiki/Variable-length_quantity][https://en.wikipedia.org/wiki/Variable-length_quantity]] - [[https://blogs.infosupport.com/a-primer-on-vlq/][https://blogs.infosupport.com/a-primer-on-vlq/]] -*For* *an* *excellent* *implementation* *of* *this* *algorithm* *look* *here:* +Kwa* ** *utekelezaji bora* *wa* *hii* *algorithm* *tazama* *hapa:* - [[https://github.com/go-audio/midi/blob/master/varint.go][https://github.com/go-audio/midi/blob/master/varint.go]]