最近在一次采访中有人问我这个问题。
给定以下代码,静态整数的最小和最大可能值是num
多少?
import java.util.ArrayList;
import java.util.List;
public class ThreadTest {
private static int num = 0;
public static void foo() {
for (int i = 0; i < 5; i++) {
num++;
}
}
public static void main(String[] args) throws Exception{
List<Thread> threads = new ArrayList<Thread>();
for (int i = 0; i < 5; i++) {
Thread thread = new Thread(new Task());
threads.add(thread);
thread.start();
}
for (int i = 0; i < 5; i++) {
threads.get(i).join();
}
// …
Run Code Online (Sandbox Code Playgroud) 我使用以下代码来流式传输我的Twitter帐户收到的邮件 - :
from tweepy import Stream
from tweepy import OAuthHandler
from tweepy import API
from tweepy.streaming import StreamListener
# These values are appropriately filled in the code
consumer_key = ""
consumer_secret = ""
access_token = ""
access_token_secret = ""
class StdOutListener( StreamListener ):
def __init__( self ):
self.tweetCount = 0
def on_connect( self ):
print("Connection established!!")
def on_disconnect( self, notice ):
print("Connection lost!! : ", notice)
def on_data( self, status ):
print("Entered on_data()")
print(status, flush = True)
return True …
Run Code Online (Sandbox Code Playgroud) 根据这个答案,<<
为C风格数组重载输出操作符的正确方法是这样的 - :
#include <iostream>
using namespace std;
template <size_t arrSize>
std::ostream& operator<<( std::ostream& out, const char( &arr )[arrSize] )
{
return out << static_cast<const char*>( arr ); // use the original version
}
// Print an array
template<typename T1, size_t arrSize>
std::ostream& operator <<( std::ostream& out, const T1( & arr )[arrSize] )
{
out << "[";
if ( arrSize )
{
const char* separator = "";
for ( const auto& element : arr )
{ …
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用Spark的GraphX库实现拓扑排序.
这是我到目前为止编写的代码:
MyObject.scala
import java.util.ArrayList
import scala.collection.mutable.Queue
import org.apache.spark.SparkConf
import org.apache.spark.SparkContext
import org.apache.spark.graphx.Edge
import org.apache.spark.graphx.EdgeDirection
import org.apache.spark.graphx.Graph
import org.apache.spark.graphx.Graph.graphToGraphOps
import org.apache.spark.graphx.VertexId
import org.apache.spark.rdd.RDD
import org.apache.spark.rdd.RDD.rddToPairRDDFunctions
object MyObject {
def main(args: Array[String]): Unit = {
val conf = new SparkConf().setAppName("Spark-App").setMaster("local[2]")
val sc = new SparkContext(conf)
val resources: RDD[Resource] = makeResources(sc)
val relations: RDD[Relation] = makeRelations(sc)
println("Building graph ...")
var graph = buildGraph(resources, relations, sc)
println("Graph built!!")
println("Testing topo sort ...")
val topoSortResult = topoSort(graph, sc);
println("topoSortResult = …
Run Code Online (Sandbox Code Playgroud) functional-programming scala topological-sort apache-spark spark-graphx
我正在尝试过滤转义特殊字符的字符串并将其转换为小写。例如:"Good morning!"
转换为good morning
。
我当时将一个字符串传递给我的函数。
我已成功过滤了英语字符串,但传递本地语言字符串时出现问题。
如果要包含所有utf-8字符,应该使用哪种类型的正则表达式过滤器字符串?
#include <string>
#include <iostream>
#include <regex>
#include <algorithm>
std::string process(std::string s) {
std::string st;
std::regex r(R"([^\W_]+(?:['_-][^\W_]+)*)");
std::sregex_iterator i = std::sregex_iterator(s.begin(), s.end(), r);
std::smatch m = *i;
st = m.str();
std::transform(st.begin(), st.end(), st.begin(), ::tolower);
return st;
}
int main() {
std::string st = "?žuolas!";
std::cout << process(st) << std::endl; // <- gives: uolas
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我不断产生大量的数字,以后需要迭代。
将它们添加到数组然后使用,std::sort()
或将它们添加到heap
(优先级队列),然后将其弹出,会更有效吗?
目前,我只是有一个vector
我要推迟的。另一个数据结构是否更适合按需排序?因此问题是,在log(n)
每个插入(nlogn
)处插入n次n堆比在事实之后(也nlogn
)进行排序要快吗?
我正在使用Aerospike Python CLient放置并获得一些键值对:
aerospike_dca_client.put(key,bin)
...
...
bls_key_a = (AEROSPIKE_NAMESPACE,SET_NAME,bls_key)
hp_bid_str_a = aerospike_dca_client.get(bls_key_a)[3];
#Terminates at the line above
Run Code Online (Sandbox Code Playgroud)
但是在put之后,我的程序在Get
命令期间以SEGFAULT终止.以下是我得到的堆栈跟踪GDB
:
Program received signal SIGSEGV, Segmentation fault.
as_shm_node_get (cluster=0x353bd90508, ns=0x7fffffffc851 "test", digest=0x7fffffffc8e1 "/\231!\221h\223\240\021KX\377\357O\346u\214q\356\267#\177", write=false, replica=AS_POLICY_REPLICA_MASTER)
at src/main/aerospike/as_shm_cluster.c:431
431 src/main/aerospike/as_shm_cluster.c: No such file or directory.
in src/main/aerospike/as_shm_cluster.c
Missing separate debuginfos, use: debuginfo-install blas-3.2.1-4.el6.x86_64
(gdb) bt
#0 as_shm_node_get (cluster=0x353bd90508, ns=0x7fffffffc851 "test", digest=0x7fffffffc8e1 "/\231!\221h\223\240\021KX\377\357O\346u\214q\356\267#\177", write=false, replica=AS_POLICY_REPLICA_MASTER)
at src/main/aerospike/as_shm_cluster.c:431
#1 0x00007fffdf219ce5 in as_node_get (cluster=0x353bd90508, err=0x7fffffffc920, cn=0x7fffffffc7c0, command=0x7fffffffc740 "\002\003", command_len=82, timeout_ms=1000, retry=1,
parse_results_fn=0x7fffdf21a97b <as_command_parse_result>, …
Run Code Online (Sandbox Code Playgroud) 我想通过循环遍历所有元素并调用delete
每个元素来删除动态分配的数组.
(我这样做是因为我必须将一个数组"移动"到另一个位置,即复制原始数组然后将其删除,但这比同时复制每个元素并delete
单独调用它们要花费2倍的时间)
我有以下代码:
int main()
{
int *n=new int[2];
delete n;
delete (n+1);
}
Run Code Online (Sandbox Code Playgroud)
但每次运行时我都会收到分段错误....
虽然,这很好 - :
int main()
{
int *n=new int[1];
delete n;
}
Run Code Online (Sandbox Code Playgroud)
所以,我的猜测是删除以某种方式删除整个数组而不是单个元素!
任何人都可以解释我的猜测是否正确,如果是的话,建议一个可能的解决方法?
我在Ubuntu 13.04上使用GCC 4.7.3
我有以下代码:
def modify_dict(my_dict):
my_dict = {'b': 2}
print(my_dict)
def main():
my_dict = {'a': 1}
modify_dict(my_dict)
print(my_dict)
if __name__ == '__main__':
main()
Run Code Online (Sandbox Code Playgroud)
它的输出是:
{'b': 2}
{'a': 1}
Run Code Online (Sandbox Code Playgroud)
我的问题是为什么对函数内部字典所做的更改没有反映在函数中main()
?
另外,如何更新函数内部的字典,以便更改反映在函数外部?
我的脚本中有两个工作。一旦作业开始,其他作业将异步运行。我为此使用了线程。这个线程会返回一些信息,而其他线程会计算这些信息。
我想要做的是在计数器的值发生变化的同时,线程也继续运行。
我想要的显示:
-----------------------------------------
Count: 5
-----------------------------------------
thread keeps running...
thread keeps running...
thread keeps running...
Run Code Online (Sandbox Code Playgroud)
实际上我使用curses
模块实现了这个目标,但这并不是我想要的。因为当我按下^C
终端内容时。我希望它们冻结在屏幕上。
带有诅咒的代码:
-----------------------------------------
Count: 5
-----------------------------------------
thread keeps running...
thread keeps running...
thread keeps running...
Run Code Online (Sandbox Code Playgroud)
有没有办法在不使用curses或不丢失内容的curses的情况下实现相同的目标?
谢谢!
python ×4
c++ ×3
aerospike ×1
algorithm ×1
apache-spark ×1
arrays ×1
c++14 ×1
dictionary ×1
function ×1
java ×1
linux ×1
ncurses ×1
object ×1
performance ×1
pointers ×1
pretty-print ×1
python-3.x ×1
regex ×1
scala ×1
sorting ×1
spark-graphx ×1
templates ×1
tweepy ×1
twitter ×1
unicode ×1
utf-8 ×1