如果一台计算机只能容纳100万个号码,如何找出1亿个号码的中位数?
algorithm complexity-theory computer-science median memory-efficient
我有以下两个模型:School和User,以及它们之间的HABTM关系,带有连接表.
在这个连接表的外键指的用户表中不叫user_id,但student_id.
class School < ActiveRecord::Base
has_and_belongs_to_many :students, :class_name => "User", :join_table => "schools_students", :foreign_key => "student_id"
end
class User < ActiveRecord::Base
has_and_belongs_to_many :studying_schools, :class_name => "School", :join_table => "schools_students", :foreign_key => "school_id"
end
Run Code Online (Sandbox Code Playgroud)
我想在我的用户和学校设备中创建一个学校和一个用户,但在User中定义的foreign_key似乎是个问题.
fixtures/users.yml :
user1:
name: Student
studying_schools: school1
Run Code Online (Sandbox Code Playgroud)
fixtures/schools.yml :
school1:
name: School 1
active: true
students: user1
Run Code Online (Sandbox Code Playgroud)
加载上面的灯具会返回ActiveRecord异常:
ActiveRecord::StatementInvalid: Mysql::Error: Unknown column 'user_id' in 'field list': INSERT INTO schools_students (student_id, user_id) VALUES (6562055, 14302562)
我究竟做错了什么 ?
是否有处理树木的模块或功能?我有一个看起来像这样的类型:
type t =
Leaf of string (* todo: replace with 'a *)
| Node of string * t list
Run Code Online (Sandbox Code Playgroud)
我正在努力插入,删除子树等
我用谷歌但找不到任何东西.
我正在尝试编写一个包装器shell脚本,每次调用命令时都会缓存信息.它只需要存储第一个非选项参数.例如,在
$ mycommand -o option1 -f another --spec more arg1 arg2
Run Code Online (Sandbox Code Playgroud)
我想要检索"arg1".
怎么能在bash中完成?
假设我有一个这样的列表:
['one','two','three','four','five','six','seven','eight','nine']
Run Code Online (Sandbox Code Playgroud)
我想尝试将这些数据转换为各种维度的HTML表格:
one two three
four five six
seven eight nine
Run Code Online (Sandbox Code Playgroud)
要么
one four seven
two five eight
three six nine
Run Code Online (Sandbox Code Playgroud)
要么
one two three four
five six seven eight
nine
Run Code Online (Sandbox Code Playgroud)
是否有一个库可以处理这个,而不需要做疯狂的列表拼接或嵌套的循环?我的谷歌搜索显示有一些HTML库,但我没有时间浏览每一个,看看他们是否可以很好地处理表.有没有人不得不这样做?如果是这样你是怎么做到的?
我正在处理的代码是WPF应用程序的一部分,该应用程序应该显示一组用户可以与之交互的扑克牌堆栈.所以我正在使用三个UserControl:
在MainControl.xaml中:
<StackPanel
Grid.Row="0"
Orientation="Horizontal">
<ItemsControl
ItemsSource="{Binding Path=CardStacks}"
>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ItemsControl>
</StackPanel>
Run Code Online (Sandbox Code Playgroud)
我的数据模板在Resources.xaml文件中定义:
<DataTemplate x:Key="MainTemplate" DataType="{x:Type ViewModel:MainViewModel}">
<View:MainControl />
</DataTemplate>
<DataTemplate DataType="{x:Type ViewModel:CardStackViewModel}">
<View:CardStackControl />
</DataTemplate>
<DataTemplate x:Key="CardTemplate" DataType="{x:Type ViewModel:CardViewModel}">
<View:CardControl />
</DataTemplate>
Run Code Online (Sandbox Code Playgroud)
CardStackControl.xaml:
<StackPanel Orientation="Vertical">
<TextBlock
Height="30"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Text="{Binding Path=CardName}"
/>
<ItemsControl
Height="Auto"
ItemsSource="{Binding Path=Cards}"
ItemTemplate="{StaticResource CardTemplate}"
>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ItemsControl>
<TextBlock
Height="30"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Text="{Binding Path=Count}"
/>
</StackPanel>
Run Code Online (Sandbox Code Playgroud)
CardControl.xaml:
<TextBlock
Name="TitleTextBlock"
Grid.Row="0"
Grid.Column="1"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Text="{Binding Path=Name}" …Run Code Online (Sandbox Code Playgroud) 我需要编写一个脚本来向我展示我在某些配置文件中设置的所有别名,如.tcshrc和一些私有脚本,以防我忘记别名的含义,如"a","b"等.配置文件中别名的格式是别名"别名a的内容"或别名b'别名b'的内容.
我写的代码如下:
#! /usr/bin/perl
open ( F , "<" , ".tcshrc" ) or die "can't open it; $! " ;
while ( <F> ) {
if ( /\b(alias\s+\w+\s+[\'\"][^\'\"]*[\'\"])/ ) {
print $1 ;
}
Run Code Online (Sandbox Code Playgroud)
但代码不起作用.那么你们中的任何人都可以查看代码并告诉我reqex有什么问题吗?
@KarelBílek是的,我错过了第二次反击,现在它奏效了.但我仍然有兴趣知道是否有更好的方法来编写正则表达式.
@Charles我想要匹配的行就像
alias a 'ls -l'
alias b "rm *"
Run Code Online (Sandbox Code Playgroud) 这可能是一个愚蠢的问题,但我如何从我的Cocoa应用程序执行shell命令?
我将命令作为字符串"命令",但可以根据需要轻松操作数据.
无需获取返回的输出值.
我的类包含一个连接到服务器的套接字.该类的一些方法可以抛出异常.我正在运行的脚本包含一个外部循环,它捕获异常,记录错误,并创建一个尝试重新连接到服务器的新类实例.
问题是服务器一次只处理一个连接(按设计),并且"旧"套接字仍然连接.因此,新的连接尝试会挂起脚本.我可以通过强制关闭旧套接字解决这个问题,但我想知道:为什么套接字不会自动关闭?
当它"卡住"时,netstat显示连接到端口的两个插槽.服务器正在等待来自第一个套接字的输入,但它还没有处理新的套接字.
我在一个虚拟服务器上运行它,它向每个传入的行回复"error \n".
编辑:请参阅我对Mark Rushakoff的回答.在异常处理程序中的断言(False)[我随后捕获]似乎强制套接字关闭.
import socket
class MyException(Exception):
pass
class MyClient(object):
def __init__(self, port):
self.sock = socket.create_connection(('localhost', port))
self.sockfile = self.sock.makefile()
def do_stuff(self):
self._send("do_stuff\n")
response = self._receive()
if response != "ok\n":
raise MyException()
return response
def _send(self, cmd):
self.sockfile.write(cmd)
self.sockfile.flush()
def _receive(self):
return self.sockfile.readline()
def connect():
c = MyClient(9989)
# On the second iteration, do_stuff() tries to send data and
# hangs indefinitely.
print c.do_stuff()
if __name__ == '__main__':
for _ in xrange(3):
try:
connect() …Run Code Online (Sandbox Code Playgroud) 我正在尝试修复Windows上的CRLF问题.
如何查看core.autocrlf系统上配置设置的值是什么?