Exercise: Stringers

Make the IPAddr type implement fmt.Stringer to print the address as a dotted quad.

For instance, IPAddr{1, 2, 3, 4} should print as "1.2.3.4".
 1 package main
 2
 3 import "fmt"
 4
 5 type IPAddr [4]byte
 6
 7 // TODO: Add a "String() string" method to IPAddr.
 8
 9 func (ip IPAddr) String() string {
10     return fmt.Sprintf("%v.%v.%v.%v", ip[0], ip[1], ip[2], ip[3])
11 }
12
13 func main() {
14     hosts := map[string]IPAddr{
15         "loopback":  {127, 0, 0, 1},
16         "googleDNS": {8, 8, 8, 8},
17     }
18     for name, ip := range hosts {
19         fmt.Printf("%v: %v\n", name, ip)
20     }
21 }
时间: 2024-11-09 00:43:10

Exercise: Stringers的相关文章

Exercise: Maps (单词统计)

A Tour of Go Exercise: Maps https://tour.golang.org/moretypes/23 WordCount (单词统计) 是一个很经典的小程序了,在很多编程入门教程中都会出现. 这道题比较简单,但也有一些知识点值得一提. 上面这个答案我是参考了网上别人写的.但在参考别人之前我也自己解题了,其中,唯一不同之处是这一句: m[word]++ 我本来写的是: _, ok := m[word] if ok { m[word]++ } else { m[word]

Exercise: Slices (画图)

A Tour of Go Exercise: Slices https://tour.golang.org/moretypes/18 这道题目,提供了一个画图函数 (pic.Show), 可以生成图片. 这个函数,即 pic.Show(f func(int, int) [][]uint8), 可见,它接受一个函数做参数,题目要求的正是编写这个参数.答案如下: 这里面,依赖一个 package, 即 "golang.org/x/tour/pic" 我上 https://github.co

Stanford coursera Andrew Ng 机器学习课程编程作业(Exercise 2)及总结

Exercise 1:Linear Regression---实现一个线性回归 关于如何实现一个线性回归,请参考:http://www.cnblogs.com/hapjin/p/6079012.html Exercise 2:Logistic Regression---实现一个逻辑回归 问题描述:用逻辑回归根据学生的考试成绩来判断该学生是否可以入学. 这里的训练数据(training instance)是学生的两次考试成绩,以及TA是否能够入学的决定(y=0表示成绩不合格,不予录取:y=1表示录

【DeepLearning】Exercise:PCA in 2D

Exercise:PCA in 2D 习题的链接:Exercise:PCA in 2D pca_2d.m close all %%================================================================ %% Step 0: Load data % We have provided the code to load data from pcaData.txt into x. % x is a 2 * 45 matrix, where the

kk Exercise 6.1 Convert an integer to words

Exercise 6-1. write a program that will prompt for and read a positive integer less than1,000 from the keyboard, and then create and output a string that is the value of theinteger in words. For example, if 941 is entered, the program will create the

Exercise 3.3 Calculate a discounted price

Exercise 3-3. Write a program that will calculate the price for a quantity entered from the keyboard, given that the unit price is $5 and there is a discount of 10 percent for quantities over 30 and a 15 percent discount for quantities over 50. 1 //

[Usaco2010 Dec]Exercise 奶牛健美操

[Usaco2010 Dec]Exercise 奶牛健美操 题目 Farmer John为了保持奶牛们的健康,让可怜的奶牛们不停在牧场之间 的小路上奔跑.这些奶牛的路径集合可以被表示成一个点集和一些连接 两个顶点的双向路,使得每对点之间恰好有一条简单路径.简单的说来, 这些点的布局就是一棵树,且每条边等长,都为1. 对于给定的一个奶牛路径集合,精明的奶牛们会计算出任意点对路径的最大值, 我们称之为这个路径集合的直径.如果直径太大,奶牛们就会拒绝锻炼. Farmer John把每个点标记为1..V

Exercise 2.3 Calculating volume price of alternative products

// Exercise 2.3 Calculating volume price of alternative products // The only problem here is to devise a way to determine the price // for the product type. Here I used the product type value to do this. #include <stdio.h> int main(void) { double to

Exercise 2.1 Convert inches to yards, feet, and inches

Exercise 2-1. Write a program that prompts the user to enter a distance in inches andthen outputs that distance in yards, feet, and inches. (For those unfamiliar with imperialunits, there are 12 inches in a foot and 3 feet in a yard.) #include <stdio