google大赛 入围赛250分真题
Problem Statement
You have a collection of music files with names formatted as “genre-artist-album-song” (quotes for clarity only), where genre, artist, album, and song each consist of only lowercase letters (‘a’-'z’) and spaces (but no leading/trailing spaces). The collection is given in the String[] collection. You would like to filter the songs according to a set of criteria given in the String[] filterInfo. Each element of filterInfo is an equality check formatted as “field=value” (quotes for clarity only), where field is “genre”, “artist”, “album”, or “song”, and value consists of only lowercase letters (‘a’-'z’) and spaces (but no leading/trailing spaces). For a file to pass through the filter, it must satisfy every equality check in filterInfo. For example, if filterInfo = {“genre=country”, “album=greatest hits”}, only songs from country greatest hits albums should be returned. Return a String[] containing all the files that meet the given criteria in the same relative order as they appear in collection.
Definition
Class:SongFilter
Method:filter
Parameters:String[], String[]
Returns:String[]
Method signature:
String[] filter(String[] collection, String[] filterInfo)
(be sure your method is public)
Constraints
collection will contain between 1 and 50 elements, inclusive.
Each element of collection will be formatted as described in the problem statement.
Each element of collection will contain between 7 and 50 characters, inclusive.
Each genre, artist, album, and song in collection will contain between 1 and 20 characters, inclusive.
collection will contain no duplicate elements.
filterInfo will contain between 1 and 4 elements, inclusive.
Each element of filterInfo will be formatted as described in the problem statement.
Each value in filterInfo will contain between 1 and 20 characters, inclusive.
Examples
0)
{“jazz-joe pass-virtuoso-cherokee”,
”rock-led zeppelin-ii-lemon song”,
”country-dwight yoakam-long way home-things change”,
”metal-iron maiden-powerslave-aces high”,
”pop-supremes-more hits-ask any girl”,
”rock-faith no more-angel dust-rv”,
”jazz-chuck mangione-feels so good-feels so good”,
”rock-van halen-ii-spanish fly”}
{“genre=rock”, “album=ii”}
Returns: {“rock-led zeppelin-ii-lemon song”, “rock-van halen-ii-spanish fly” }
This filter returns all the rock songs from albums with the title “ii”.
1)
{“rock-jimi hendrix-axis bold as love-little wing”,
”rock-cars-cars-moving in stereo”,
”rock-jimi hendrix-electric ladyland-gypsy eyes”,
”blues-albert collins-ice pickin-ice pick”,
”rock-jimi hendrix-axis bold as love-bold as love”,
”rock-jimi hendrix-axis bold as love-exp”}
{“artist=jimi hendrix”, “album=axis bold as love”}
Returns:
{“rock-jimi hendrix-axis bold as love-little wing”,
”rock-jimi hendrix-axis bold as love-bold as love” }
This filter returns all the songs that are from the album “axis bold as love” by the artist “jimi hendrix”. The last element in the collection is not returned because there are two spaces between “jimi” and “hendrix”.
2)
{“rock-ozzy osbourne-blizzard of ozz-dee”,
”rock-marvelous three-hey album-let me go”,
”rock-cheap trick-in color-downed”}
{“genre=soul”}
Returns: { }
There is no soul music in this collection, so an empty String[] is returned.
3)
{“country-topcoder-the country album-twangy”,
”rock-topcoder-the rock album-rockin”,
”jazz-topcoder-the jazz album-jazzy”,
”soul-topcoder-the soul album-soulful”,
”metal-topcoder-the metal album-thrash”}
{“artist=topcoder”, “genre=jazz”, “album=the jazz album”, “song=jazzy”}
Returns: {“jazz-topcoder-the jazz album-jazzy” }
4)
{“pop-jackson five-abc-the love you save”,
”rock-ac dc-powerage-riff raff”}
{“genre=pop”, “genre=rock”}
Returns: { }
No single element of collection can represent more than one genre, so this filter returns an empty String[].
This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.
题目来源:http://www.vcgood.com/archives/145
Python代码:
# -*- coding: cp936 -*-
class SongFilter:
def __init__(self,all_songs,criterias):
self.song_list = []
self.criteria_list = []
self.bad_criteria_flag = 0 #规则为错误规则的标志位 #将每首歌曲中间的连字符去掉,并组成列表;self.song_list为所有歌曲列表组成的总列表
for song in all_songs:
self.song_list.append(song.split('-')) #将每个规则中间的等号符去掉,并组成元组;self.criteria_list为所有规则元组组成的总列表
for criteria in criterias:
self.criteria_list.append(tuple(criteria.split('='))) #判断规则是否有误
length = len(self.criteria_list)
for i in range(length-1):
for j in range(i+1,length):
if self.criteria_list[i][0] == self.criteria_list[j][0]:
self.bad_criteria_flag = 1 #将规则元组组成的列表转化为字典存放
self.criteria_dict = dict(self.criteria_list) def filter(self):
self.filter_songs = []
self.result = [] #将符合规则的歌曲(没有连字符)存入self.filter_songs
for song in self.song_list:
if (song[0] == self.criteria_dict.get('genre') or self.criteria_dict.get('genre') == None):
if (song[1] == self.criteria_dict.get('artist') or self.criteria_dict.get('artist') == None):
if (song[2] == self.criteria_dict.get('album') or self.criteria_dict.get('album') == None):
if (song[3] == self.criteria_dict.get('song') or self.criteria_dict.get('song') == None):
self.filter_songs.append(song) #为过滤出的歌曲增加连字符
for song in self.filter_songs:
self.result.append( '-'.join(song)) if self.bad_criteria_flag == 1:
return []
return self.result
运行结果:
>>> all_songs = ['jazz-joe pass-virtuoso-cherokee',
'rock-led zeppelin-ii-lemon song',
'country-dwight yoakam-long way home-things change',
'metal-iron maiden-powerslave-aces high',
'pop-supremes-more hits-ask any girl',
'rock-faith no more-angel dust-rv',
'jazz-chuck mangione-feels so good-feels so good',
'rock-van halen-ii-spanish fly']
>>> criterias = ['genre=rock', 'album=ii']
>>> sf = SongFilter(all_songs,criterias)
>>> sf.filter()
['rock-led zeppelin-ii-lemon song', 'rock-van halen-ii-spanish fly']
>>> all_songs = ['rock-jimi hendrix-axis bold as love-little wing',
'rock-cars-cars-moving in stereo',
'rock-jimi hendrix-electric ladyland-gypsy eyes',
'blues-albert collins-ice pickin-ice pick',
'rock-jimi hendrix-axis bold as love-bold as love',
'rock-jimi hendrix-axis bold as love-exp']
>>> criterias = ['artist=jimi hendrix', 'album=axis bold as love']
>>> sf = SongFilter(all_songs,criterias)
>>> sf.filter()
['rock-jimi hendrix-axis bold as love-little wing', 'rock-jimi hendrix-axis bold as love-bold as love']
>>> all_songs = ['rock-ozzy osbourne-blizzard of ozz-dee',
'rock-marvelous three-hey album-let me go',
'rock-cheap trick-in color-downed']
>>> criterias = ['genre=soul']
>>> sf = SongFilter(all_songs,criterias)
>>> sf.filter()
[]
>>> all_songs = ['country-topcoder-the country album-twangy',
'rock-topcoder-the rock album-rockin',
'jazz-topcoder-the jazz album-jazzy',
'soul-topcoder-the soul album-soulful',
'metal-topcoder-the metal album-thrash']
>>> criterias = ['artist=topcoder', 'genre=jazz', 'album=the jazz album', 'song=jazzy']
>>> sf = SongFilter(all_songs,criterias)
>>> sf.filter()
['jazz-topcoder-the jazz album-jazzy']
>>> all_songs = ['pop-jackson five-abc-the love you save',
'rock-ac dc-powerage-riff raff']
>>> criterias = ['genre=pop', 'genre=rock']
>>> sf = SongFilter(all_songs,criterias)
>>> sf.filter()
[]
google大赛 入围赛250分真题的更多相关文章
- 第九届蓝桥杯省赛c/c++真题明码题解答案,另类excel解法思路
直到快比赛才重视起之前学校给报了蓝桥杯,且这段时间一直在做Python,所以没做什么准备. 赛场上做这道题时连反码补码的知识点都记混,所以直接用了excel做这道题目,分享下做题思路.及题解. 标题: ...
- 第十届蓝桥杯JavaB组省赛真题
试题 A: 组队 本题总分:5 分 [问题描述] 作为篮球队教练,你需要从以下名单中选出 1 号位至 5 号位各一名球员, 组成球队的首发阵容. 每位球员担任 1 号位至 5 号位时的评分如下表所示. ...
- Python解答蓝桥杯省赛真题之从入门到真题(二刷题目一直更新)
蓝桥刷题 原文链接: https://github.com/libo-sober/LanQiaoCup Python解答蓝桥杯省赛真题之从入门到真题 不同字串 """ 一 ...
- 2016 第七届蓝桥杯 c/c++ B组省赛真题及解题报告
2016 第七届蓝桥杯 c/c++ B组省赛真题及解题报告 勘误1:第6题第4个 if最后一个条件粗心写错了,答案应为1580. 条件应为abs(a[3]-a[7])!=1,宝宝心理苦啊.!感谢zzh ...
- 第九届蓝桥杯JavaC组决(国)赛真题
1:年龄问题 s夫人一向很神秘.这会儿有人问起她的年龄,她想了想说: "20年前,我丈夫的年龄刚好是我的2倍,而现在他的年龄刚好是我的1.5倍". 你能算出s夫人现在的年龄吗? 这 ...
- Noip前的大抱佛脚----Noip真题复习
Noip前的大抱佛脚----Noip真题复习 Tags: Noip前的大抱佛脚 Noip2010 题目不难,但是三个半小时的话要写四道题还是需要码力,不过按照现在的实力应该不出意外可以AK的. 机器翻 ...
- 第四届蓝桥杯 c/c++真题
第四届蓝桥杯 c/c++真题 <1>高斯日记 问题 大数学家高斯有个好习惯:无论如何都要记日记. 他的日记有个与众不同的地方,他从不注明年月日,而是用一个整数代替,比如:4210 后来人们 ...
- 第三届蓝桥杯 c/c++真题
第三届蓝桥杯真题 c/c++ 以下题目我自己也并不是所有的题目都是一次性就能做对或是有结题思路的.有些题目也是经过查证网上相关的资料或是参考了别人的代码和解题思路才做出来的.总的来看,这份题目考了很多 ...
- 蓝桥杯java历年真题及答案整理1~20.md
蓝桥杯java历年真题及答案整理(闭关一个月,呕心沥血整理出来的) 1 算法是这样的,如果给定N个不同字符,将这N个字符全排列,最终的结果将会是N!种.如:给定 A.B.C三个不同的字符,则结果为:A ...
随机推荐
- 2016/09/21 java关键字static
1.static方法 static方法一般称作静态方法,由于静态方法不依赖于任何对象就可以进行访问,因此对于静态方法来说,是没有this的,因为它不依附于任何对象,既然都没有对象,就谈不上th ...
- Hadoop伪分布式搭建CentOS
所需软件及版本: jdk-7u80-linux-x64.tar.gz hadoop-2.6.0.tar.gz 1.安装JDK Hadoop 在需在JDK下运行,注意JDK最好使用Oracle的否则可能 ...
- linux下的mount命令的用法详解
挂接命令(mount) 首先,介绍一下挂接(mount)命令的使用方法,mount命令参数非常多,这里主要讲一下今天我们要用到的. 命令格式:mount [-t vfstype] [-o option ...
- instanceof、==号、Objetc类
1)instanceof: 判断某个对象是否为某个类的实例,注意多态的运用,一个父类引用指向子类的对象,根据继承,子类就是父类,所以子类也可以看做是父类的一个实例. 形式:引用 instanceof ...
- [转]linux时间同步
转自:http://www.jotop.com/2012/vpsinfo_0525/439.html 美国的vps大多都是国外的时间,让我们的程序总是不适应.那么如何调整linux的时间为北京时间?修 ...
- QWidget设置为模态问题
设置QWidget的Qt::WindowModality属性为Qt::WindowModal和Qt::ApplicationModal,发现窗体仍然不会模态,网上查了一下,有人说改属性只对window ...
- verilog 奇数分频设计
module tw(clk,k_or,k1,k2); input clk; output k_or,k1,k2; reg [2:0] c1,c2; reg m1,m2; initial begin c ...
- JAVA栈实例—括号匹配
import java.util.Stack; public class test { public static void main(String[] args){ System.out.print ...
- 0-N背包为题(动态规划算法)
/****************0-N背包问题****************** * 有n个物体装入容量为c的背包,每一个物体有一个体积 * 和一个价值,所装入的物体体积之和不大于背包体积, * ...
- OA Framework - How to Find the Correct Version of JDeveloper to Use with E-Business Suite 11i or Release 12.x (Doc ID 416708.1)
APPLIES TO: Oracle Applications Framework - Version 11.5.10.0 to 12.2.2 [Release 11.5.10 to 12.2] In ...