When someone says AI will steal everybody's jobs

const symbols = {
"I": 1,
"V": 5,
"X": 10,
"L": 50,
"C": 100,
"D": 500,
"M": 1000
};

const romanToInt = function(s) {
let value = 0;
for (let i = 0; i < s.length - 1; i++) {
console.log("s", s[i])
console.log("s+1", s[i+1])
symbols[s[i]] < symbols[s[i+1]] ? value -= symbols[s[i]]: value += symbols[s[i]];

    console.log("value", value)

}
return value + symbols[s[s.length-1]];
};

Input: "XXIV"

expected output: 24

Chat GPT answers...

---

The function is called with the input string "XXIV". value is initialized to 0.

value is 0.

The for loop iterates over each character in the input string, except for the last character.

In the first iteration, the current character is "X" and the next character is "X", so value is incremented by 10, becoming 10.

In the second iteration, the current character is "X" and the next character is "I", so value is decremented by 10, becoming 0. Then value is decremented by 1, becoming -1.

In the third iteration, the current character is "I" and the next character is "V", so value is incremented by 1, becoming 0. Then value is incremented by 5, becoming 5.

After the loop has processed all but the last character in the Roman numeral, the value of the last character ("V") is added to value, which becomes 24.

The final value of value is returned, which represents the integer value of the input Roman numeral. In this case, the final value of value is 24, which is the correct integer value of the Roman numeral "XXIV".

reply

Other posts you might like

Leetcode - Add Two Numbers - Two Solutions

You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order, and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list.

Full problem here.

Solution 1. Based on this solution

var addTwoNumbers = function(l1, l2) {
    const head = new ListNode(0)

    let firstNode = l1
    let secondNode = l2
    let currentNode = head
    let carry = 0

    while (firstNode || secondNode || carry) {
        const firstValue = firstNode?.val || 0
        const secondValue = secondNode?.val || 0
leetcodeprogrammingjavascriptrecursionlinked listsdata structures
reply

successful spells and potions. Leetcode problem: https://leetcode.com/problems/successful-pairs-of-spells-and-potions/description/

--------------------

Input: spells = [5,1,3], potions = [1,2,3,4,5], success = 7 Output: [4,0,3] Explanation:

  • 0th spell: 5 * [1,2,3,4,5] = [5,10,15,20,25]. 4 pairs are successful.
  • 1st spell: 1 * [1,2,3,4,5] = [1,2,3,4,5]. 0 pairs are successful.
  • 2nd spell: 3 * [1,2,3,4,5] = [3,6,9,12,15]. 3 pairs are successful. Thus, [4,0,3] is returned.

Intuition

My initial intuition (and I assume for many others out there), when seeing the problem, is to multiply each spell by each potion in the potions array, return the number of successful pairs and continue in this way. Unfortunately, this leads to a time complexity of O(n * m) which will not work for bigger inputs. In order to change the time complexity from O(n*m) to O(n * log(m)) we have to use a binary search to minimize the number of searches through the potions array we have to do.

Approach

We initiate a helper function called spellAndPotions which accepts a single spell, an array of potions and success/target number. It returns a number "successfulPairs" which is a counter for the number of successfulPairs of a single spell * potion in the potion array. The spellAndPotions function uses binary search in order to minimize the number of potions it has to analyze, therefore it expects the potions array to be sorted for the input.

successfulPairs function is initiated and accepts a spells ...

reply

Leetcode - Capacity To Ship Packages Within D Days - Two Solutions

Here are my two solutions to today's Leetcode problem:

Both solutions use binary search, they are inspired by this solution, with slight performance improvements. Both of them rely on a greedy algorithm and binary search.

Solution 1:

This solution beats 98.92% of solutions in terms of runtime speed but only 21.8% in terms of memory usage.

const isPossible = (weights, days, maxShipSize) => {
  let currentDay = 1;
  let sumOfWeights = 0;

  for (let i = 0; i < weights.length; i++) {
    const weight = weights[i];
    const newSum = sumOfWeights + weight;

    if (newSum > maxShipSize) {
leetcodeprogramming
reply

It's to the tune of Wellerman

(Verse 1) There once was a crew of devs, me boys, Coding away, they'd find their joys, They'd spin up servers, make sites fast, And hope their code would always last.

(Chorus) Soon may the Web Devs come, To bring us code and make sites run, One day, when the coding's done, We'll take our leave and go.

(Verse 2) They'd write in JavaScript, CSS, HTML would form the base, no less, Frameworks, libraries, all combined, To create sites that'd blow your mind.

1 comment

The Thai Letter ซ and Its Usage for Beginners

Today, we will explore the Thai consonant ซ, which is equated with the "s" sound in English, as in the word "sun." The letter ซ is named "สอ ซอ" in Thai and emanates a sound somewhere between an English "s" and "z," almost always veering towards a soft "s" sound. It is important to note that, unlike English, Thai syllables are pronounced with equal stress, so the pitch of your voice must remain consistent across the syllable.

Now, let's introduce three common Thai words that contain the consonant ซ:

  1. ทำซ้ำ (tam sâam) - to repeat or do again
  2. ซื้อ (súe) - to buy
  3. ด้านซ้าย (dâan sáai) - left side

In order to help you understand how these words are used in everyday Thai, let's see them in simple sentences:

  1. ฉันต้องทำซ้ำเพราะไม่ถูกต้องครั้งแรก (Chăn dtông tam sâam práw mâi tòok tong kráng râek.) I have to repeat it because it wasn’t correct the first time.

  2. เธอจะไปซื้อของที่ตลาด (Ter jà bpai súe kŏng têe talàd.) She will go to buy things at the market.

thai languagethai alphabetbeginner thaithai vocabularythai letter ซlanguage learningthai
reply

How I implemented slugs on Sanity - a TypeScript code sample

The lack of human-readable slugs on Sanity had bothered me for a while and I finally got around to fixing them last Sunday. The old, slugless URL structure probably wasn't doing me any favors in terms of SEO and user experience. I'm hoping the new format can give Sanity a much needed SEO boost. Plus, I can finally tell which post is which in Google Search Console and Vercel Analytics.

The Result

Before

https://www.sanity.media/p/64c375049f5d6b05859f10c6

After

https://www.sanity.media/p/64c375049f5d6b05859f10c6-delicious-post-workout-milkshake-recipe

Isn't this much clearer?

The Code

When writing the code I had the following goals in mind:

programmingjavascriptmongoosebuilding in publicmongodb
1 comment

The Thai Letter ฟ: An Introduction for Beginners

Understanding the Letter ฟ

The Thai alphabet consists of 44 consonants, and the letter is one of them. It is the 36th letter in the Thai alphabet and is called "ฟัน" (fan) which translates to "tooth" in English. The character symbolizes the sound of the English letter 'F' and is categorized under low-class consonants in the Thai language.

Pronunciation

The pronunciation of ฟ is similar to the 'f' sound as in the English words "fish" or "fun." When pronouncing ฟ, place your upper teeth gently on your lower lip and release air to create the sound. It's a voiceless sound, meaning you don't use your vocal cords while pronouncing it.

Thai Vocabulary with ฟ

Here are some beginner-level Thai words that use the letter ฟ:

  • ไฟไหม้ (fai mài) - Fire, meaning "burning"
  • ฟรี (free) - Free, as in no cost
  • ลิฟท์ (lift) - Lift, or elevator

These words provide a glimpse of how ฟ is used in Thai and allow for practice with simple vocabulary.

thai languagethai alphabetthailearn thailanguage learningthai letter ฟbeginner thaithai consonants
1 comment

Introduction to Beginner Mandarin: 写 (Xiě), 再见 (Zàijiàn), and 下车 (Xiàchē)

写 (xiě)

The character 写 (xiě) means "to write." It consists of two parts: the top part, called the radical, is called 冖 (miànzhào), which was originally associated with a cover or shelter. The bottom part is the character 与 (yǔ), which means "to give." Combined, they create the concept of writing or describing something, as if giving form to thoughts under the shelter of the mind.

Example Sentences:

  1. 我要写信。(Wǒ yào xiě xìn.)
    • I want to write a letter.
  2. 他在写作业。(Tā zài xiě zuòyè.)
    • He is doing his homework.
  3. 请写下你的名字。(Qǐng xiě xià nǐ de míngzì.)
    • Please write down your name.

再见 (zàijiàn)

再见 (zàijiàn), translating to "goodbye" or "see you again," is made up of two characters. 再 (zài) means "again," and 见 (jiàn) means "to see." Essentially, you're expressing a wish to see the person again in the future when you part ways.

mandarinbeginner mandarinhsk 1foreign languageslanguage learninglearn mandarinchinese characters
reply

Understanding the Thai Letter ไ

Pronunciation of ไ

The symbol "ไ" represents the long vowel sound /ai/ (as in "eye"). It's important to note that "ไ" always precedes the consonant it's associated with, which might be a bit of a twist for English speakers, as in Thai script, vowels can be written before, above, below, after, or even around the consonants.

When "ไ" is combined with a consonant, it forms a syllable that sounds like "ai" followed by the consonant sound. For example, when paired with the consonant "ก" (g), it would be pronounced like "ไก" and sound similar to the English word "guy."

Introduction to Vocabulary Words

Let's look at some basic Thai words that include the vowel symbol "ไ":

  1. จำได้ว่า (jam dâi wâ) – means "remember that"
  2. ไมล์ (mai) – means "mile" and it is a borrowed word from English
  3. ได้ยิน (dâi yin) – means "to hear"

Each of these words includes the "ไ" sound, which is a helpful practice in getting comfortable with this aspect of Thai pronunciation.

Using the Words in Sentences

thai languagethai vocabularythai letter ไthaithai alphabetlearn thaibeginner thailanguage learning
1 comment

Understanding the Thai Character ื (Sara Ue)

Today, we'll focus on a particular vowel character that many beginners find intriguing: ื or "Sara Ue."

Pronunciation of ื

The vowel ื is transcribed in the Roman alphabet as "ue" and pronounced like the 'ue' in the English word 'blue,' but shorter. It's vital to note that in Thai, vowels can be written before, after, below, or above the consonants they follow. For ื (Sara Ue), it is written above the consonant.

Common Words with ื

Let's look at some basic words that use this vowel:

  1. เลื่อย (lêuay) - This word means "saw," as in the tool you use to cut wood.
  2. หรือ (rĕu) - This is a versatile word that means "or" but can also be used to form questions, similar to "right?" in English.
  3. เชื่อ (chêua) - Meaning "believe" or "trust," it is a foundational word in building statements about faith or conviction.

Simple Sentences Using เลื่อย, หรือ, and เชื่อ

  1. เขาใช้เลื่อยตัดไม้. (khăo chái lêuay dtàt mái)

    • He uses a saw to cut wood.
  2. คุณจะดื่มชาหรือกาแฟ? (khun jà dèum chaa rĕu gaa-fae?)

thai languagethaithai alphabetforeign languageslanguage learningthai vocabularybeginner thaithai letter ื
reply
feedback