[PYTHON] 03. Pi

03. Pi

Break down the sentence "Now I need a drink, alcoholic of course, after the heavy lectures involving quantum mechanics." Into words, and create a list of the number of characters (in the alphabet) of each word in order of appearance.

Go

package main

import (
	"fmt"
	"regexp"
	"strings"
)

func main() {
	var src string = "Now I need a drink, alcoholic of course, after the heavy lectures involving quantum mechanics."
	var wlen []int
	rex := regexp.MustCompile("([a-zA-Z]+)")

	//Divide into words
	words := strings.Split(src, " ")
	for _, word := range words {
		//Extract only the alphabet
		match := rex.FindStringSubmatch(word)

		//Save the number of letters in the alphabet
		wlen = append(wlen, len(match[1]))
	}

	//View results
	fmt.Println(wlen)
}

python

# -*- coding: utf-8 -*-
import re

src = "Now I need a drink, alcoholic of course, after the heavy lectures involving quantum mechanics."
wlen = []

#Regular expression pattern definition (alphabet only)
pattern = re.compile('([a-zA-Z]+)')

#Divide into words
words = src.split(" ")
for word in words:
    #Extract only alphabet
    result = re.search(pattern, word).group()

    #Save the number of letters in the alphabet
    wlen.append(len(result))

#View results
print(wlen)

Javascript

var src = "Now I need a drink, alcoholic of course, after the heavy lectures involving quantum mechanics."
var wlen = [];

//Loop version
var words = src.split(' ');
for (var i = 0; i < words.length; i++) {
    val = words[i].match(/[a-zA-Z]+/);
    wlen.push(val[0].length);
}
console.log(wlen);


//forEach version
var wlen = [];
src.split(' ').forEach(function (value) {
    this.push(value.match(/[a-zA-Z]+/)[0].length);
}, wlen)

console.log(wlen);

Summary

I made it as easy as other than, but I noticed that it was wrong when I saw the results of other people. Do you omit "." And ","? I was a little worried about alphabet discrimination, but I used regular expressions. Was Go's regular expression processing slow? ..

I was somehow happy when I understood the meaning of the pi in question. w

Top

Recommended Posts

03. Pi
Raspberry Pi backup
What is Raspberry Pi?
pigpio on Raspberry pi
Raspberry Pi video camera
Raspberry Pi Bad Knowledge
Let's do Raspberry Pi?
DigitalSignage with Raspberry Pi
Raspberry Pi 4 setup memo
Cython on Raspberry Pi
Raspberry Pi system monitoring